c#獲取機器硬體資訊

暖楓無敵發表於2014-09-01
  1. using System;  
  2. using System.Management;  
  3. namespace Soyee.Comm  
  4. {  
  5.     /// <summary>  
  6.     /// Computer Information  
  7.     /// </summary>  
  8.     public class Computer  
  9.     {  
  10.         public string CpuID;  
  11.         public string MacAddress;  
  12.         public string DiskID;  
  13.         public string IpAddress;  
  14.         public string LoginUserName;  
  15.         public string ComputerName;  
  16.         public string SystemType;  
  17.         public string TotalPhysicalMemory; //單位:M  
  18.         private static Computer _instance;  
  19.         public static Computer Instance()  
  20.         {  
  21.             if (_instance == null)  
  22.                 _instance = new Computer();  
  23.             return _instance;  
  24.         }  
  25.         protected  Computer()  
  26.         {  
  27.             CpuID=GetCpuID();  
  28.             MacAddress=GetMacAddress();  
  29.             DiskID=GetDiskID();  
  30.             IpAddress=GetIPAddress();  
  31.             LoginUserName=GetUserName();  
  32.             SystemType=GetSystemType();  
  33.             TotalPhysicalMemory=GetTotalPhysicalMemory();  
  34.             ComputerName=GetComputerName();  
  35.         }  
  36.         string  GetCpuID()  
  37.         {  
  38.             try  
  39.             {  
  40.                 //獲取CPU序列號程式碼  
  41.                 string cpuInfo = "";//cpu序列號  
  42.                 ManagementClass mc = new ManagementClass("Win32_Processor");  
  43.                 ManagementObjectCollection moc = mc.GetInstances();  
  44.                 foreach(ManagementObject mo in moc)  
  45.                 {  
  46.                     cpuInfo = mo.Properties["ProcessorId"].Value.ToString();  
  47.                 }  
  48.                 moc=null;  
  49.                 mc=null;  
  50.                 return cpuInfo;  
  51.             }  
  52.             catch  
  53.             {  
  54.                 return "unknow";  
  55.             }  
  56.             finally  
  57.             {  
  58.             }  
  59.               
  60.         }  
  61.         string  GetMacAddress()  
  62.         {  
  63.             try  
  64.             {  
  65.                 //獲取網路卡硬體地址  
  66.                 string mac="";  
  67.                 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");  
  68.                 ManagementObjectCollection moc = mc.GetInstances();  
  69.                 foreach(ManagementObject mo in moc)  
  70.                 {  
  71.                     if((bool)mo["IPEnabled"] == true)  
  72.                     {  
  73.                         mac=mo["MacAddress"].ToString();  
  74.                         break;  
  75.                     }  
  76.                 }  
  77.                 moc=null;  
  78.                 mc=null;  
  79.                 return mac;  
  80.             }  
  81.             catch  
  82.             {  
  83.                 return "unknow";  
  84.             }  
  85.             finally  
  86.             {  
  87.             }  
  88.               
  89.         }  
  90.         string  GetIPAddress()  
  91.         {  
  92.             try  
  93.             {  
  94.                 //獲取IP地址  
  95.                 string st="";  
  96.                 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");  
  97.                 ManagementObjectCollection moc = mc.GetInstances();  
  98.                 foreach(ManagementObject mo in moc)  
  99.                 {  
  100.                     if((bool)mo["IPEnabled"] == true)  
  101.                     {  
  102.                         //st=mo["IpAddress"].ToString();  
  103.                         System.Array ar;  
  104.                         ar=(System.Array)(mo.Properties["IpAddress"].Value);  
  105.                         st=ar.GetValue(0).ToString();  
  106.                         break;  
  107.                     }  
  108.                 }  
  109.                 moc=null;  
  110.                 mc=null;  
  111.                 return st;  
  112.             }  
  113.             catch  
  114.             {  
  115.                 return "unknow";  
  116.             }  
  117.             finally  
  118.             {  
  119.             }  
  120.               
  121.         }  
  122.            
  123.         string  GetDiskID()  
  124.         {  
  125.             try  
  126.             {  
  127.                 //獲取硬碟ID  
  128.                 String HDid="";  
  129.                 ManagementClass mc = new ManagementClass("Win32_DiskDrive");  
  130.                 ManagementObjectCollection moc = mc.GetInstances();  
  131.                 foreach(ManagementObject mo in moc)  
  132.                 {  
  133.                     HDid = (string)mo.Properties["Model"].Value;  
  134.                 }  
  135.                 moc=null;  
  136.                 mc=null;  
  137.                 return HDid;  
  138.             }  
  139.             catch  
  140.             {  
  141.                 return "unknow";  
  142.             }  
  143.             finally  
  144.             {  
  145.             }  
  146.                   
  147.         }  
  148.   
  149.         /// <summary>  
  150.         /// 作業系統的登入使用者名稱  
  151.         /// </summary>  
  152.         /// <returns></returns>  
  153.         string  GetUserName()  
  154.         {  
  155.             try  
  156.             {  
  157.                 string st="";  
  158.                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");  
  159.                 ManagementObjectCollection moc = mc.GetInstances();  
  160.                 foreach(ManagementObject mo in moc)  
  161.                 {  
  162.                       
  163.                     st=mo["UserName"].ToString();  
  164.                       
  165.                 }  
  166.                 moc=null;  
  167.                 mc=null;  
  168.                 return st;  
  169.             }  
  170.             catch  
  171.             {  
  172.                 return "unknow";  
  173.             }  
  174.             finally  
  175.             {  
  176.             }  
  177.               
  178.         }  
  179.   
  180.   
  181.         /// <summary>  
  182.         /// PC型別  
  183.         /// </summary>  
  184.         /// <returns></returns>  
  185.         string  GetSystemType()  
  186.         {  
  187.             try  
  188.             {  
  189.                 string st="";  
  190.                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");  
  191.                 ManagementObjectCollection moc = mc.GetInstances();  
  192.                 foreach(ManagementObject mo in moc)  
  193.                 {  
  194.                       
  195.                     st=mo["SystemType"].ToString();  
  196.                       
  197.                 }  
  198.                 moc=null;  
  199.                 mc=null;  
  200.                 return st;  
  201.             }  
  202.             catch  
  203.             {  
  204.                 return "unknow";  
  205.             }  
  206.             finally  
  207.             {  
  208.             }  
  209.               
  210.         }  
  211.   
  212.         /// <summary>  
  213.         /// 實體記憶體  
  214.         /// </summary>  
  215.         /// <returns></returns>  
  216.         string  GetTotalPhysicalMemory()  
  217.         {  
  218.             try  
  219.             {  
  220.                   
  221.                 string st="";  
  222.                 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");  
  223.                 ManagementObjectCollection moc = mc.GetInstances();  
  224.                 foreach(ManagementObject mo in moc)  
  225.                 {  
  226.                       
  227.                     st=mo["TotalPhysicalMemory"].ToString();  
  228.                       
  229.                 }  
  230.                 moc=null;  
  231.                 mc=null;  
  232.                 return st;  
  233.             }  
  234.             catch  
  235.             {  
  236.                 return "unknow";  
  237.             }  
  238.             finally  
  239.             {  
  240.             }  
  241.         }  
  242.         /// <summary>  
  243.         ///   
  244.         /// </summary>  
  245.         /// <returns></returns>  
  246.         string  GetComputerName()  
  247.         {  
  248.             try  
  249.             {                 
  250.                 return System.Environment.GetEnvironmentVariable("ComputerName");  
  251.             }  
  252.             catch  
  253.             {  
  254.                 return "unknow";  
  255.             }  
  256.             finally  
  257.             {  
  258.             }  
  259.         }  
  260.   
  261.   
  262.     }  
  263. }

相關文章