标签:
感觉话说的多了,费劲。
public class ComputerInfo { public ComputerInfo() {} /// <summary> /// cpu型号 /// </summary> /// <returns></returns> public string GetCpuInfo() { ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_Processor"); foreach (ManagementObject obj2 in searcher.Get()) { try { return (obj2.GetPropertyValue("Name").ToString() + "," + obj2.GetPropertyValue("CurrentClockSpeed").ToString() + " Mhz," + Environment.ProcessorCount.ToString() + " 个处理器"); } catch { continue; } } return "未知"; } /// <summary> /// cpuID /// </summary> /// <returns></returns> public string GetCpuID() { ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances(); foreach (ManagementObject obj2 in instances) { return obj2.Properties["ProcessorId"].Value.ToString(); } return null; } /// <summary> /// 取得设备硬盘的卷标号 /// </summary> /// <returns></returns> public string GetDiskVolumeSerialNumber() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } /// <summary> /// 获得CPU的序列号 /// </summary> /// <returns></returns> public string getCpu() { string strCpu = null; ManagementClass myCpu = new ManagementClass("win32_Processor"); ManagementObjectCollection myCpuConnection = myCpu.GetInstances(); foreach (ManagementObject myObject in myCpuConnection) { strCpu = myObject.Properties["Processorid"].Value.ToString(); break; } return strCpu; } /// <summary> /// 生成机器码 /// </summary> /// <returns></returns> public string getMNum() { string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号 string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码 return strMNum; }
/// <summary> /// 物理内存 /// </summary> /// <returns></returns> public string GetTotalPhysicalMemory() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo.ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } /// <summary> /// 获取计算机名(ComputerName) /// </summary> /// <returns></returns> public string GetComputerName() { try { return System.Environment.GetEnvironmentVariable("ComputerName"); } catch { return "unknow"; } } /// <summary> /// PC类型 /// </summary> /// <returns></returns> public string GetSystemType() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo.ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } /// <summary> /// 操作系统的登录用户名 /// </summary> /// <returns></returns> public string GetUserName() { try { string st = ""; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { st = mo.ToString(); } moc = null; mc = null; return st; } catch { return "unknow"; } } }
备注写的很清楚,只是做一个存档。
标签:
原文地址:http://www.cnblogs.com/JeffConrtoller/p/4705047.html