获取本机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-18 11:42  朱小勇  阅读(313)  评论(0编辑  收藏  举报