欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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;
}

上一篇:

下一篇: