1. 程式人生 > >獲取本機IP

獲取本機IP

AD new pad address cal 獲取本機 etl foreach 形式

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

獲取本機IP