获取服务器的IP地址方法以DNS法较为简单实用
如下
private void ButtonIP_Click(object sender SystemEventArgs e)
{ Syste
mNetIPAddress[] addressList = DnsGetHostByName(DnsGetHostName())AddressList;
if ( addressListLength>)
{ TextLIPText = addressList[]ToString();
TextSIPText = addressList[]ToString();
}
else
{
TextLIPText = addressList[]ToString();
TextSIPText = 没有可用的连接;
}
}
获取服务器的IP地址与MAC地址另一方法如下
using SystemManagement;
string stringMAC = ;
string stringIP = ;
ManagementClass MC = new ManagementClass Win_NetworkAdapterConfiguration);
ManagementObjectCollection MOC= MCGetInstances();
foreach(ManagementObject MO in MOC)
{
if ((bool)MO[IPEnabled] == true)
{
stringMAC += MO[MACAddress]ToString();
TextMACText = stringMACToString();
string[] IPAddresses = (string[]) MO[IPAddress];
if(IPAddressesLength > )
stringIP = IPAddresses[];
TextIPText = stringIPToString();
}
}
获取客户端本机的IP地址相当简易方法如下
using SystemNet;TextIPText=PageRequestUserHostAddress;
如要获取客户端本机的MAC地址就相对复杂些得导入调用两个API用ARP协议获取但这样只能获取到同网段机器的MAC对于跨网段的得利用IP扫描或cmd中nbtstat命令获取MAC地址也可通过读取系统注册表值或WMI的数据库来获取各位如有简易可行的好办法请留言告诉我