获取本机IP,返回字符串

     public static String GetLocalIp()
        {
            String[] Ips = GetLocalIpAddress();

            foreach (String ip in Ips) if (ip.StartsWith("10.80.")) return ip;
            foreach (String ip in Ips) if (ip.Contains(".")) return ip;

            return "127.0.0.1";
        }
        public static String[] GetLocalIpAddress()
        {
            string hostName = Dns.GetHostName();                    //获取主机名称  
            IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主机IP地址  

            string[] IP = new string[addresses.Length];             //转换为字符串形式  
            for (int i = 0; i < addresses.Length; i++) IP[i] = addresses[i].ToString();

            return IP;
        }

 

posted @ 2018-05-22 15:42  朱小勇  阅读(297)  评论(0编辑  收藏  举报