获取本地IP

来源:互联网 发布:python入门马哥教育 编辑:程序博客网 时间:2024/06/07 16:14

方法一:

foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)            {                if (ip.AddressFamily.ToString() == "InterNetwork")                {                    MessageBox.Show(ip.ToString());                    break;                }            }

方法二:

IPHostEntry ipEntry = Dns.GetHostByName(Dns.GetHostName());//GetHostByName()方法已过时            string ip = ipEntry.AddressList[0].ToString();            MessageBox.Show(ip);


方法三:

string ip = System.Net.Dns.Resolve(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();//System.Net.Dns.Resolve()方法已过时            MessageBox.Show(ip);

方法四:

IPGlobalProperties ipInfo = IPGlobalProperties.GetIPGlobalProperties();            //返回有关本地计算机上的 Internet 协议版本 4 (IPV4) 传输控制协议 (TCP) 连接的信息。             TcpConnectionInformation[] ipStaticIp4 = ipInfo.GetActiveTcpConnections();            IPEndPoint localEndPoint = ipStaticIp4[ipStaticIp4.Length - 1].LocalEndPoint;            string ip = localEndPoint.Address.ToString();            MessageBox.Show(ip);

欢迎补充……











0 0