一个用C#获取硬件信息的类

来源:互联网 发布:网络药品销售促成技巧 编辑:程序博客网 时间:2024/04/25 16:54
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Management;
  7. namespace GetPCInfo
  8. {
  9.     class HardInfoClass
  10.     {
  11.         [DllImport("kernel32.dll")]
  12.         private static extern int GetVolumeInformation(
  13.             string lpRootPathName,
  14.             string lpVolumeNameBuffer,
  15.             int nVolumeNameSize,
  16.             ref int lpVolumeSerialNumber,
  17.             int lpMaximumComponentLength,
  18.             int lpFileSystemFlags,
  19.             string lpFileSystemNameBuffer,
  20.             int nFileSystemNameSize
  21.         );
  22.         public HardInfoClass()
  23.         {
  24.             //    
  25.             // TODO: 在此处添加构造函数逻辑    
  26.             //    
  27.         }
  28.         /// <summary>
  29.         /// 获取机器名
  30.         /// </summary>
  31.         /// <returns></returns>
  32.         public string GetHostName()
  33.         {
  34.             return System.Net.Dns.GetHostName();
  35.         }
  36.         /// <summary>
  37.         /// 获得主机IP地址
  38.         /// </summary>
  39.         /// <returns></returns>
  40.         public string GetHostIP()
  41.         {
  42.             return System.Net.Dns.GetHostAddresses(GetHostName())[0].ToString();
  43.         }
  44.         /// <summary>
  45.         /// 获取CPU编号   
  46.         /// </summary>
  47.         /// <returns></returns>
  48.         public string GetCpuID()
  49.         {
  50.             try
  51.             {
  52.                 ManagementClass mc = new ManagementClass("Win32_Processor");
  53.                 ManagementObjectCollection moc = mc.GetInstances();
  54.                 string strCpuID = null;
  55.                 foreach (ManagementObject mo in moc)
  56.                 {
  57.                     strCpuID = mo.Properties["ProcessorId"].Value.ToString();
  58.                     break;
  59.                 }
  60.                 return strCpuID;
  61.             }
  62.             catch
  63.             {
  64.                 return "";
  65.             }
  66.         }
  67.         /// <summary>
  68.         /// 获取第一块硬盘编号 
  69.         /// </summary>
  70.         /// <returns></returns>
  71.         public string GetHardDiskID()
  72.         {
  73.             try
  74.             {
  75.                 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
  76.                 string strHardDiskID = null;
  77.                 foreach (ManagementObject mo in searcher.Get())
  78.                 {
  79.                     if (mo != null)
  80.                     {
  81.                         strHardDiskID = mo["SerialNumber"].ToString().Trim();
  82.                         break;
  83.                     }
  84.                 }
  85.                 return strHardDiskID;
  86.             }
  87.             catch(Exception ex)
  88.             {
  89.                 return "null";
  90.                 //throw new Exception(ex.Message);
  91.             }
  92.         }
  93.         /// <summary>
  94.         /// 获取网卡MAC地址 
  95.         /// </summary>
  96.         /// <returns></returns>
  97.         public string GetNetCardMAC()
  98.         {
  99.             try
  100.             {
  101.                 string stringMAC = "";
  102.                 ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
  103.                 ManagementObjectCollection MOC = MC.GetInstances();
  104.                 foreach (ManagementObject MO in MOC)
  105.                 {
  106.                     if ((bool)MO["IPEnabled"] == true)
  107.                     {
  108.                         stringMAC += MO["MACAddress"].ToString();
  109.                     }
  110.                 }
  111.                 return stringMAC;
  112.             }
  113.             catch
  114.             {
  115.                 return "";
  116.             }
  117.         }
  118.         /// <summary>
  119.         /// 获取硬盘信息的代码    
  120.         /// </summary>
  121.         /// <param name="drvID"></param>
  122.         /// <returns></returns>
  123.         public string GetVolOf(string drvID)
  124.         {
  125.             try
  126.             {
  127.                 const int MAX_FILENAME_LEN = 256;
  128.                 int retVal = 0;
  129.                 int a = 0;
  130.                 int b = 0;
  131.                 string str1 = null;
  132.                 string str2 = null;
  133.                 int i = GetVolumeInformation(
  134.                  drvID + @":/",
  135.                  str1,
  136.                  MAX_FILENAME_LEN,
  137.                  ref retVal,
  138.                  a,
  139.                  b,
  140.                  str2,
  141.                  MAX_FILENAME_LEN
  142.                  );
  143.                 return retVal.ToString("x");
  144.             }
  145.             catch
  146.             {
  147.                 return "";
  148.             }
  149.         }
  150.         /// <summary>
  151.         /// 获取当前网卡IP地址    
  152.         /// </summary>
  153.         /// <returns></returns>
  154.         public string GetNetCardIP()
  155.         {
  156.             try
  157.             {
  158.                 string stringIP = "";
  159.                 ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
  160.                 ManagementObjectCollection MOC = MC.GetInstances();
  161.                 foreach (ManagementObject MO in MOC)
  162.                 {
  163.                     if ((bool)MO["IPEnabled"] == true)
  164.                     {
  165.                         string[] IPAddresses = (string[])MO["IPAddress"];
  166.                         if (IPAddresses.Length > 0)
  167.                         {
  168.                             stringIP = IPAddresses[0].ToString();
  169.                         }
  170.                     }
  171.                 }
  172.                 return stringIP;
  173.             }
  174.             catch
  175.             {
  176.                 return "";
  177.             }
  178.         }
  179.     }
  180. }