1. 程式人生 > >C#:獲取本機的機器名,IP地址

C#:獲取本機的機器名,IP地址

public static string GetHostName()
{
	string name = System.Net.Dns.GetHostName();
	return name;            
}

public static List<string> GetIPList()
{
	string name = Dns.GetHostName();
	IPAddress[] ips = Dns.GetHostAddresses(name);
	List<string> list = new List<string>();
	foreach (IPAddress ip in ips)
	{
		if (Regex.IsMatch(ip.ToString(), @"^\d+.\d+.\d+.\d+$"))
		{
			list.Add(ip.ToString());
		}
	}
	return list;
}

需要引用:

using System.Text.RegularExpressions; using System.Net;