C # obtain the CPU serial number, MAC address, hard disk ID, local IP address, computer name, physical memory, PC type,
First introduce the service and then call
This article Reprinted from http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html
Public class Computer {public static string CpuID; // 1. cpu serial number public static string MacAddress; // 2. mac serial number public static string DiskID; // 3. hard Disk id public static string IpAddress; // 4. public static string LoginUserName; // 5. logon Username: public static string ComputerName; // 6. computer Name public static string SystemType; // 7. public static string TotalPhysicalMemory; // 8. memory usage unit: M static Computer () {CpuID = GetCpuID (); MacAddress = GetMacAddress (); DiskID = GetDiskID (); IpAddress = GetIPAddress (); LoginUserName = GetUserName (); systemType = GetSystemType (); TotalPhysicalMemory = GetTotalPhysicalMemory (); ComputerName = GetComputerName ();} // 1. code for obtaining the CPU serial number static string GetCpuID () {try {string cpuInfo = ""; // cpu serial number 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 {}// 2. obtain the NIC hardware address 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 ;}} moc = null; mc = null; return mac ;}catch {return "unknow" ;}finally {}// 3. obtain the hard disk ID static 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"]. value;} moc = null; mc = null; return HDid;} catch {return "unknow";} finally {} // 4. obtain the IP address static string GetIPAddress () {try {string st = ""; ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc. getInstances (); foreach (ManagementObject mo in moc) {if (bool) mo ["IPEnabled"] = true) {// st = mo ["IpAddress"]. toString (); System. array ar; ar = (System. array) (mo. properties ["IpAddress"]. value); st = ar. getValue (0 ). toString (); break ;}} moc = null; mc = null; return st ;}catch {return "unknow" ;}finally {}/// 5. logon username of the operating system static string GetUserName () {try {string un = ""; un = Environment. userName; return un;} catch {return "unknow";} finally {} // 6. obtain the computer name static string GetComputerName () {try {return System. environment. machineName;} catch {return "unknow";} finally {} // 7 PC Type static 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 {}/// 8. physical memory static 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 {}}}