C#获取IP比较简单,主要是以下语句:
IPAddress ServerIp = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
String ip = ServerIp.ToString(); //获得字符串形式的IP值
如果有多个IP地址(多网卡的情况),就把AddressList[0]里的0换成1或者2或者3...
侠之大者,为国为民。。接分
获取的方法见代码:
protected string GetIP() //获取本地IP
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
return ipAddr.ToString();
}
System.Net.IPAddress[] ips = System.Net.Dns.GetHostAddresses(Environment.MachineName);
string strIP = "";
for(int i=0;i
strIP += ips[i].ToString();
}
this.TextBox1.Text = strIP;