首先引入服務 然後 調用
本文轉載自http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html
public class Computer { public static string CpuID; //1.cpu序列號 public static string MacAddress; //2.mac序列號 public static string DiskID; //3.硬盤id public static string IpAddress; //4.ip地址 public static string LoginUserName; //5.登錄用戶名 public static string ComputerName; //6.計算機名 public static string SystemType; //7.系統類型 public static string TotalPhysicalMemory; //8.內存量 單位:M static Computer() { CpuID = GetCpuID(); MacAddress = GetMacAddress(); DiskID = GetDiskID(); IpAddress = GetIPAddress(); LoginUserName = GetUserName(); SystemType = GetSystemType(); TotalPhysicalMemory = GetTotalPhysicalMemory(); ComputerName = GetComputerName(); } //1.獲取CPU序列號代碼 static string GetCpuID() { try { 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 { } } //2.獲取網卡硬件地址 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.獲取硬盤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.獲取IP地址 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.操作系統的登錄用戶名 static string GetUserName() { try { string un = ""; un = Environment.UserName; return un; } catch { return "unknow"; } finally { } } //6.獲取計算機名 static string GetComputerName() { try { return System.Environment.MachineName; } catch { return "unknow"; } finally { } } ///7 PC類型 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.物理內存 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 { } } }