我想问下用什么方法可以获取本机的IP,谢谢!!
RT
------解决方案--------------------ipconfig/all
------解决方案--------------------using System.Management;
private string RegisterMAC()
{
ManagementClass adapters = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
string MACAddress = "unknown ";
foreach (ManagementObject adapter in adapters.GetInstances())
{
if ((bool)adapter[ "IPEnabled "] == true)
{
MACAddress = adapter.Properties[ "MACAddress "].Value.ToString();
break;
}
}
return MACAddress;
}
------解决方案--------------------Dns.GetHostAddresses 方法
------解决方案--------------------获取服务器的IP地址方法,如下:
using System.Net;
private void ButtonIP_Click(object sender, System.EventArgs e)
{
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
if ( addressList.Length> 1)
{ TextLIP.Text = addressList[0].ToString();
TextSIP.Text = addressList[1].ToString();
}
else
{
TextLIP.Text = addressList[0].ToString();
TextSIP.Text = "没有可用的连接 ";
}
}
------解决方案--------------------using System.Management;
这句别漏了
private string RegisterMAC()
{
ManagementClass adapters = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
string MACAddress = "unknown ";
foreach (ManagementObject adapter in adapters.GetInstances())
{
if ((bool)adapter[ "IPEnabled "] == true)
{
MACAddress = adapter.Properties[ "MACAddress "].Value.ToString();
break;
}
}
return MACAddress;
}
------解决方案--------------------JF