您好,登錄后才能下訂單哦!
如何在C#項目中獲取硬件參數(shù)?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
C# 獲取硬件參數(shù)的實現(xiàn)方法
示例代碼:
private static string GetIdentifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = ""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { if (mo[wmiMustBeTrue].ToString() == "True") { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } } return result; } private static string GetIdentifier(string wmiClass, string wmiProperty) { string result = ""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } return result; } // cpu id GetIdentifier("Win32_Processor", "UniqueId"); //processor id GetIdentifier("Win32_Processor", "ProcessorId"); //processor name GetIdentifier("Win32_Processor", "Name"); //Manufacturer GetIdentifier("Win32_Processor", "Manufacturer"); //BIOS Identifier private static string GetBiosId() { return GetIdentifier("Win32_BIOS", "Manufacturer") + GetIdentifier("Win32_BIOS", "SMBIOSBIOSVersion") + GetIdentifier("Win32_BIOS", "IdentificationCode") + GetIdentifier("Win32_BIOS", "SerialNumber") + GetIdentifier("Win32_BIOS", "ReleaseDate") + GetIdentifier("Win32_BIOS", "Version"); } //Main physical hard drive ID private static string GetDiskId() { return GetIdentifier("Win32_DiskDrive", "Model") + GetIdentifier("Win32_DiskDrive", "Manufacturer") + GetIdentifier("Win32_DiskDrive", "Signature") + GetIdentifier("Win32_DiskDrive", "TotalHeads"); } //Motherboard ID private static string GetBaseId() { return GetIdentifier("Win32_BaseBoard", "Model") + GetIdentifier("Win32_BaseBoard", "Manufacturer") + GetIdentifier("Win32_BaseBoard", "Name") + GetIdentifier("Win32_BaseBoard", "SerialNumber"); } //Primary video controller ID private static string GetVideoId() { return GetIdentifier("Win32_VideoController", "DriverVersion") + GetIdentifier("Win32_VideoController", "Name"); } //First enabled network card ID private static string GetMacId() { return GetIdentifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); }
看完上述內(nèi)容,你們掌握如何在C#項目中獲取硬件參數(shù)的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。