首页 > 代码库 > C#.Net获取Mac等PC信息
C#.Net获取Mac等PC信息
止乎于分享
using System.Management;using System.Net;
public class PCInfoHelper { /// <summary> /// 操作系统的登录用户名 /// </summary> /// <returns></returns> public static string GetUserName() { try { string username = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { username = mo["UserName"].ToString(); } return username; } catch { return "unknow"; } } /// <summary> /// 获取MAC地址 /// </summary> /// <returns></returns> public static 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; } } return mac; } catch { return "unknow"; } } /// <summary> /// 获取硬盘ID /// </summary> /// <returns></returns> public static string GetDiskID() { try { String diskid = ""; ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { diskid = (String)mo.Properties["Model"].Value.ToString(); } return diskid; } catch { return "unknow"; } } /// <summary> /// 获取CPU ID /// </summary> /// <returns></returns> public static string GetCpuID() { try { string cpuid = ""; ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuid = mo.Properties["ProcessorId"].Value.ToString(); } return cpuid; } catch { return "unknow"; } } /// <summary> /// 系统名称 /// </summary> /// <returns></returns> public static string GetSystemType() { try { string systype = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { systype = mo["SystemType"].ToString(); } return systype; } catch { return "unknow"; } } /// <summary> /// 获取物理内存 /// </summary> /// <returns></returns> public static string GetTotalPhysicalMemory() { try { string phymemory = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { phymemory = mo["TotalPhysicalMemory"].ToString(); } return phymemory; } catch { return "unknow"; } } /// <summary> /// 获取客户端内网IP地址 /// </summary> /// <returns></returns> public static string GetLocalIPAddress() { try { string localIP = null; IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipaddress = ipHost.AddressList[0]; localIP = ipaddress.ToString(); return "内网IP地址:" + localIP; } catch { return "内网IP地址:unknown"; } } /// <summary> /// 获得客户端外网IP地址 /// </summary> /// <returns>IP地址</returns> public static string GetInternetIPAddress() { try { string internetIP = ""; using (WebClient webClient = new WebClient()) { internetIP = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址 //判断IP是否合法 if (!System.Text.RegularExpressions.Regex.IsMatch(internetIP, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}")) { internetIP = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址 } } return "外网IP地址:" + internetIP; } catch { return "外网IP地址:unknown"; } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。