C#擷取硬體資訊(一)

來源:互聯網
上載者:User

C#擷取硬體資訊

//經測試通過

//需引用System.Management

using System;

using System.Management;

namespace MyCustomClassLib

{

/// <summary>

/// 硬體資訊類

/// </summary>

public class HardwareInfo

     {

        public HardwareInfo()

        { 

        }

       #region 硬體屬性

       /// <summary> 

      /// 機器名

     /// </summary>

    public string HostName

   {  

        get

        {return System.Net.Dns.GetHostName();

        } 

   } 

   /// <summary>

   /// CPU編號 

   /// </summary> 

   public string CPUID 

   { 

         get{return GetCpuID();}

   } 

ASP.NET擷取CPU序號,硬碟序號ID,擷取網卡編號
public static void GetCpuInfo(out string cpuInfo)
{
//得到cpu資訊
string _cpuInfo="";//cpu資訊
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
_cpuInfo = mo.Properties["ProcessorId"].Value.ToString();

}
cpuInfo=_cpuInfo;
}

//擷取硬碟ID
string _HDInfo="";
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach(ManagementObject mo in moc1)
{
_HDInfo = (string)mo.Properties["Model"].Value;

}
HDInfo=_HDInfo;
}

public static void GetMacAddress(out string MacAddress)
{
//擷取網卡硬體地址

string _MacAddress="";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach(ManagementObject mo in moc2)
{
if((bool)mo["IPEnabled"] == true)
MacAddress=mo["MacAddress"].ToString();
mo.Dispose();
}
MacAddress=_MacAddress;
}

學習WMI有感
通過兩天的學習發現WMI確實是個好東西:),利用它我們可以很方便的對電腦的硬體資訊進行管理,但是用它得到的硬體資訊十分有限,特別適用於對硬體資訊細節要求不高的使用者,例如一些OEM廠商對產線上的產品進行管理和檢測,有了它,從此對那些繁瑣的API說聲byebye了......

下面這幾個小例子是我這兩天的學習心得:

//Get LAN MAC address
static void GetLanMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC address/t{0}", mo["MacAddress"].ToString());
mo.Dispose();
}
Console.WriteLine ("/n/r");
}
//Get HDD Size / Serial # / Firmware
static void GetHDDInfo()
{
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;

//Console.WriteLine("HDD Serial/t{0}", mo["Serial Number"].ToString());

Console.WriteLine("HDD Size/t{0} Bytes", mo["Size"].ToString());
Console.WriteLine("HDD Model/t{0}", mo["Model"].ToString());
Console.WriteLine("HDD Manufacturer /t{0}", mo["Manufacturer"].ToString());
//Console.WriteLine("HDD Model/t{0}", mo["Firmware"].ToString());

mo.Dispose ();
}

Console.WriteLine ("/n/r");
}
//Get CPU INFO.
static void GetCpuInfo()
{
//string cpuInfo = "";//cpu序號
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
//cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Console.WriteLine("ProcessorId:/t{0}", mo["ProcessorId"].ToString());
Console.WriteLine("CurrentClockSpeed:/t{0}", mo["CurrentClockSpeed"].ToString());

//Console.WriteLine("L2CacheSize:/t{0}", mo["L2CacheSize"].ToString());
//Console.WriteLine("L2CacheSpeed:/t{0}", mo["L2CacheSpeed"].ToString());

Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("ProcessorType:/t{0}", mo["ProcessorType"].ToString());
Console.WriteLine("Version:/t{0}", mo["Version"].ToString());
Console.WriteLine("Revision:/t{0}", mo["Revision"].ToString());
Console.WriteLine("Manufacturer:/t{0}", mo["Manufacturer"].ToString());
//Console.WriteLine(cpuInfo);
}
Console.WriteLine ("/n/r");
}
//Get Cahce INFO.
static void GetCacheInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_CacheMemory");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("MaxCacheSize:/t{0}", mo["MaxCacheSize"].ToString());
Console.WriteLine("Location:/t{0}", mo["Purpose"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get Memory INFO.
static void GetMemoryInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Capacity:/t{0}", mo["Capacity"].ToString());
Console.WriteLine("DeviceLocator:/t{0}", mo["DeviceLocator"].ToString());
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get the battery INFO.
static void GetBatteryInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_Battery");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
//Console.WriteLine("Capacity:/t{0}", mo["BatteryRechargeTime"].ToString());
//Console.WriteLine("DeviceLocator:/t{0}", mo["BatteryStatus"].ToString());
Console.WriteLine("Description:/t{0}", mo["Chemistry"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get the LCD INFO.
static void GetLCDInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_DesktopMonitor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("PixelsPerXLogicalInch:/t{0}", mo["PixelsPerXLogicalInch"].ToString());
Console.WriteLine("PixelsPerYLogicalInch:/t{0}", mo["PixelsPerYLogicalInch"].ToString());
Console.WriteLine("Manufacturer:/t{0}", mo["MonitorManufacturer"].ToString ()); //[]
Console.WriteLine("MonitorType:/t{0}", mo["MonitorType"].ToString());
Console.WriteLine("ScreenHeight:/t{0}", mo["ScreenHeight"].ToString());
Console.WriteLine("ScreenWidth:/t{0}", mo["ScreenWidth"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");
}
//Get the BIOS INFO.
static void GetBIOSInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_BIOS");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Version:/t{0}", mo["Version"].ToString());
//Console.WriteLine("DeviceLocator:/t{0}", mo["BuildNumber"].ToString());
Console.WriteLine("BIOSVersion:/t{0}", mo["BIOSVersion"].ToString ()); //[]
Console.WriteLine("Manufacturer:/t{0}", mo["Manufacturer"].ToString());
Console.WriteLine("SerialNumber:/t{0}", mo["SerialNumber"].ToString());
//Console.WriteLine("InstallDate:/t{0}", (mo["InstallDate"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");
}
//Get HDD SerialNum
public static void GetHd()
{
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher();

wmiSearcher.Query = new SelectQuery(
"Win32_DiskDrive",
"",
new string[]{"PNPDeviceID"}
);
ManagementObjectCollection myCollection = wmiSearcher.Get();
ManagementObjectCollection.ManagementObjectEnumerator em =
myCollection.GetEnumerator();
em.MoveNext();
ManagementBaseObject mo = em.Current;
string id = mo.Properties["PNPDeviceID"].Value.ToString().Trim();
Console.WriteLine("My harddisk is : " + id);
}
//Get Monitor INFO.
public static void GetMonitorInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_DisplayConfiguration");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("DisplayFrequency:/t{0}", mo["DisplayFrequency"].ToString());
Console.WriteLine("PelsHeight:/t{0}", mo["PelsHeight"].ToString());
Console.WriteLine("PelsWidth:/t{0}", mo["PelsWidth"].ToString ()); //[]
//Console.WriteLine("MonitorType:/t{0}", mo["MonitorType"].ToString());
//Console.WriteLine("ScreenHeight:/t{0}", mo["ScreenHeight"].ToString());
//Console.WriteLine("ScreenWidth:/t{0}", mo["ScreenWidth"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");

}

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/wfy19850328/archive/2009/03/2**026030.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.