c#获取本机的IP地址的代码
程序员文章站
2023-12-14 18:58:46
复制代码 代码如下:public static ipaddress gethostip(){ &nbs...
复制代码 代码如下:
public static ipaddress gethostip()
{
iphostentry ipe = dns.gethostentry(dns.gethostname());
ipaddress ip = ipe.addresslist[0];
return ip;
}
private string getipaddress()
{
string str;
string result = "";
string hostname = dns.gethostname();
ipaddress[] myip = dns.gethostaddresses(hostname);
foreach (ipaddress address in myip)
{
str = address.tostring();
for (int i = 0; i < str.length; i++)
{
if (str[i] >= '0' && str[i] <= '9' || str[i] == '.') result = str;
}
}
return result;
}