C#获取本机所有IP地址(包括局域网和本机外网IP)
1 /// <summary> 2 /// 获取所有本机IP地址,包括局域网IP和本机外网IP(如果有) 3 /// </summary> 4 public static void GetAllIP() 5 { 6 IPAddress[] IP = Dns.GetHostAddresses(Dns.GetHostName()); 7 int m_count = IP.Length; 8 string m_AllIP = string.Empty; 9 for (int i = 0; i < m_count; i++) 10 { 11 m_AllIP += string.Format("{0}|", IP[i].ToString()); 12 } 13 14 Console.WriteLine(m_AllIP); 15 Console.Read(); 16 }
效果如下: