一个用C#获取硬件信息的类
来源:互联网 发布:网络药品销售促成技巧 编辑:程序博客网 时间:2024/04/25 16:54
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Management;
- namespace GetPCInfo
- {
- class HardInfoClass
- {
- [DllImport("kernel32.dll")]
- private static extern int GetVolumeInformation(
- string lpRootPathName,
- string lpVolumeNameBuffer,
- int nVolumeNameSize,
- ref int lpVolumeSerialNumber,
- int lpMaximumComponentLength,
- int lpFileSystemFlags,
- string lpFileSystemNameBuffer,
- int nFileSystemNameSize
- );
- public HardInfoClass()
- {
-
-
-
- }
-
-
-
-
- public string GetHostName()
- {
- return System.Net.Dns.GetHostName();
- }
-
-
-
-
- public string GetHostIP()
- {
- return System.Net.Dns.GetHostAddresses(GetHostName())[0].ToString();
- }
-
-
-
-
- public string GetCpuID()
- {
- try
- {
- ManagementClass mc = new ManagementClass("Win32_Processor");
- ManagementObjectCollection moc = mc.GetInstances();
- string strCpuID = null;
- foreach (ManagementObject mo in moc)
- {
- strCpuID = mo.Properties["ProcessorId"].Value.ToString();
- break;
- }
- return strCpuID;
- }
- catch
- {
- return "";
- }
- }
-
-
-
-
- public string GetHardDiskID()
- {
- try
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
- string strHardDiskID = null;
- foreach (ManagementObject mo in searcher.Get())
- {
- if (mo != null)
- {
- strHardDiskID = mo["SerialNumber"].ToString().Trim();
- break;
- }
- }
- return strHardDiskID;
- }
- catch(Exception ex)
- {
- return "null";
-
- }
- }
-
-
-
-
- public string GetNetCardMAC()
- {
- try
- {
- string stringMAC = "";
- ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection MOC = MC.GetInstances();
- foreach (ManagementObject MO in MOC)
- {
- if ((bool)MO["IPEnabled"] == true)
- {
- stringMAC += MO["MACAddress"].ToString();
- }
- }
- return stringMAC;
- }
- catch
- {
- return "";
- }
- }
-
-
-
-
-
- public string GetVolOf(string drvID)
- {
- try
- {
- const int MAX_FILENAME_LEN = 256;
- int retVal = 0;
- int a = 0;
- int b = 0;
- string str1 = null;
- string str2 = null;
- int i = GetVolumeInformation(
- drvID + @":/",
- str1,
- MAX_FILENAME_LEN,
- ref retVal,
- a,
- b,
- str2,
- MAX_FILENAME_LEN
- );
- return retVal.ToString("x");
- }
- catch
- {
- return "";
- }
- }
-
-
-
-
- public string GetNetCardIP()
- {
- try
- {
- string stringIP = "";
- ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection MOC = MC.GetInstances();
- foreach (ManagementObject MO in MOC)
- {
- if ((bool)MO["IPEnabled"] == true)
- {
- string[] IPAddresses = (string[])MO["IPAddress"];
- if (IPAddresses.Length > 0)
- {
- stringIP = IPAddresses[0].ToString();
- }
- }
- }
- return stringIP;
- }
- catch
- {
- return "";
- }
- }
- }
- }