首页 > 编程 > C# > 正文

c#获取本机的IP地址的代码

2020-01-24 03:32:53
字体:
来源:转载
供稿:网友

复制代码 代码如下:

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表