//c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存。 using System.Management; /// 操作系统的登录用户名: string GetUserName() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["UserName"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } } //获取MAC地址: string GetMacAddress() { try { //获取网卡硬件地址 string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { mac = mo["MacAddress"].ToString(); break; } } moc = null; mc = null; return mac; } catch { return "unknow"; } finally { } } //获取IP地址: /// <summary> /// 获取客户端内网IP地址 /// </summary> /// <returns></returns> private static string getClientLocalIPAddress() { string localIP = null; try { IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());// Dns.Resolve(Dns.GetHostName()); ; IPAddress ipaddress ; for (int i = 0; i < ipHost.AddressList.Length; i++) { ipaddress = ipHost.AddressList[i]; if (ipaddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { localIP = ipaddress.ToString(); break; } } //IPAddress.AddressFamily 属性 //对于 IPv4,返回 InterNetwork;对于 IPv6,返回 InterNetworkV6。 if (localIP==null) { localIP = ipHost.AddressList[0].ToString(); } return localIP; } catch { return "内网IP地址:unknown"; } finally { } } /// <summary> /// 获得客户端外网IP地址 /// </summary> /// <returns>IP地址</returns> public static string getClientInternetIPAddress() { string internetAddress = ""; try { using (WebClient webClient = new WebClient()) { internetAddress = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址 //判断IP是否合法 if (!System.Text.RegularExpressions.Regex.IsMatch(internetAddress, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}")) { internetAddress = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址 } } return "外网IP地址:" + internetAddress; } catch { return "外网IP地址:unknown"; } finally { } } //获取硬盘ID: string GetDiskID() { try { String HDid = ""; ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { //HDid = (string)mo.Properties["Model"].ToString(); HDid = (String)mo.Properties["Model"].Value.ToString(); } moc = null; mc = null; return HDid; } catch { return "unknow"; } finally { } } //获取CPUid: string GetCpuID() { try { //获取CPU序列号代码 string cpuInfo = "";//cpu序列号 ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); } moc = null; mc = null; return cpuInfo; } catch { return "unknow"; } finally { } } //系统名称: string GetSystemType() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["SystemType"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } } //获取物理内存: string GetTotalPhysicalMemory() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo["TotalPhysicalMemory"].ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } finally { } }