C#取得本机地址

来源:互联网 发布:java初级面试数据结构 编辑:程序博客网 时间:2024/04/29 12:50
public static string GetIp()
        {
            IPHostEntry MyIp = new IPHostEntry();
            //MyIp = Dns.Resolve(Dns.GetHostName());
            MyIp = Dns.Resolve(Dns.GetHostName());
            if (MyIp.AddressList.Length == 1)
            {
                return MyIp.AddressList[0].ToString();
            }
            else
            {
                return MyIp.AddressList[1].ToString();
            }
        }