当前位置:   article > 正文

使用 C# 获取计算机硬件信息_c# 获取mac 主板型号

c# 获取mac 主板型号

今天我们向您展示如何使用 C# 获取计算机硬件信息。您可以获取您的系统信息,如处理器 ID、硬盘序列号、系统 MAC 地址、主板制造商、主板产品 ID、CD-DVD 驱动器路径、BIOS 制造商、BIOS 序列号、BIOS 标题、系统帐户名称、物理 RAM 内存, 主板上的 RAM 插槽编号, CPU 制造商, CPU 的当前时钟速度, 默认 IP 网关, CPU 速度, 获取当前语言, 系统信息, 处理器信息, 获取计算机名称。

以下是使用 C# 获取计算机硬件信息的步骤

创建一个 Windows 窗体应用程序

如果您熟悉 Windows 窗体,那么您非常了解如何创建 Windows 窗体应用程序。
了解更多:如何在 VISUAL STUDIO 2010 中创建 WINDOWS 窗体应用程序

添加参考

添加“ System.Management ”的引用以使用C#获取计算机硬件信息。
请参见下图以在项目中添加参考。

 添加参考 

添加“System.Management”的引用

添加类文件

您可能会阅读:将参数从一个窗口传递到另一个窗口 c#

在您的项目中将类文件名添加为“ HardwareInfo.cs ”。在您的类文件中添加以下代码。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Management;
  6. public static class HardwareInfo
  7. {
  8. ///
  9. /// Retrieving Processor Id.
  10. ///
  11. ///
  12. ///
  13. public static String GetProcessorId()
  14. {
  15. ManagementClass mc = new ManagementClass("win32_processor");
  16. ManagementObjectCollection moc = mc.GetInstances();
  17. String Id = String.Empty;
  18. foreach (ManagementObject mo in moc)
  19. {
  20. Id = mo.Properties["processorID"].Value.ToString();
  21. break;
  22. }
  23. return Id;
  24. }
  25. ///
  26. /// Retrieving HDD Serial No.
  27. ///
  28. ///
  29. public static String GetHDDSerialNo()
  30. {
  31. ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk");
  32. ManagementObjectCollection mcol = mangnmt.GetInstances();
  33. string result = "";
  34. foreach (ManagementObject strt in mcol)
  35. {
  36. result += Convert.ToString(strt["VolumeSerialNumber"]);
  37. }
  38. return result;
  39. }
  40. ///
  41. /// Retrieving System MAC Address.
  42. ///
  43. ///
  44. public static string GetMACAddress()
  45. {
  46. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  47. ManagementObjectCollection moc = mc.GetInstances();
  48. string MACAddress = String.Empty;
  49. foreach (ManagementObject mo in moc)
  50. {
  51. if (MACAddress == String.Empty)
  52. {
  53. if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
  54. }
  55. mo.Dispose();
  56. }
  57. MACAddress = MACAddress.Replace(":", "");
  58. return MACAddress;
  59. }
  60. ///
  61. /// Retrieving Motherboard Manufacturer.
  62. ///
  63. ///
  64. public static string GetBoardMaker()
  65. {
  66. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
  67. foreach (ManagementObject wmi in searcher.Get())
  68. {
  69. try
  70. {
  71. return wmi.GetPropertyValue("Manufacturer").ToString();
  72. }
  73. catch { }
  74. }
  75. return "Board Maker: Unknown";
  76. }
  77. ///
  78. /// Retrieving Motherboard Product Id.
  79. ///
  80. ///
  81. public static string GetBoardProductId()
  82. {
  83. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
  84. foreach (ManagementObject wmi in searcher.Get())
  85. {
  86. try
  87. {
  88. return wmi.GetPropertyValue("Product").ToString();
  89. }
  90. catch { }
  91. }
  92. return "Product: Unknown";
  93. }
  94. ///
  95. /// Retrieving CD-DVD Drive Path.
  96. ///
  97. ///
  98. public static string GetCdRomDrive()
  99. {
  100. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_CDROMDrive");
  101. foreach (ManagementObject wmi in searcher.Get())
  102. {
  103. try
  104. {
  105. return wmi.GetPropertyValue("Drive").ToString();
  106. }
  107. catch { }
  108. }
  109. return "CD ROM Drive Letter: Unknown";
  110. }
  111. ///
  112. /// Retrieving BIOS Maker.
  113. ///
  114. ///
  115. public static string GetBIOSmaker()
  116. {
  117. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
  118. foreach (ManagementObject wmi in searcher.Get())
  119. {
  120. try
  121. {
  122. return wmi.GetPropertyValue("Manufacturer").ToString();
  123. }
  124. catch { }
  125. }
  126. return "BIOS Maker: Unknown";
  127. }
  128. ///
  129. /// Retrieving BIOS Serial No.
  130. ///
  131. ///
  132. public static string GetBIOSserNo()
  133. {
  134. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
  135. foreach (ManagementObject wmi in searcher.Get())
  136. {
  137. try
  138. {
  139. return wmi.GetPropertyValue("SerialNumber").ToString();
  140. }
  141. catch { }
  142. }
  143. return "BIOS Serial Number: Unknown";
  144. }
  145. ///
  146. /// Retrieving BIOS Caption.
  147. ///
  148. ///
  149. public static string GetBIOScaption()
  150. {
  151. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
  152. foreach (ManagementObject wmi in searcher.Get())
  153. {
  154. try
  155. {
  156. return wmi.GetPropertyValue("Caption").ToString();
  157. }
  158. catch { }
  159. }
  160. return "BIOS Caption: Unknown";
  161. }
  162. ///
  163. /// Retrieving System Account Name.
  164. ///
  165. ///
  166. public static string GetAccountName()
  167. {
  168. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");
  169. foreach (ManagementObject wmi in searcher.Get())
  170. {
  171. try
  172. {
  173. return wmi.GetPropertyValue("Name").ToString();
  174. }
  175. catch { }
  176. }
  177. return "User Account Name: Unknown";
  178. }
  179. ///
  180. /// Retrieving Physical Ram Memory.
  181. ///
  182. ///
  183. public static string GetPhysicalMemory()
  184. {
  185. ManagementScope oMs = new ManagementScope();
  186. ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
  187. ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
  188. ManagementObjectCollection oCollection = oSearcher.Get();
  189. long MemSize = 0;
  190. long mCap = 0;
  191. // In case more than one Memory sticks are installed
  192. foreach (ManagementObject obj in oCollection)
  193. {
  194. mCap = Convert.ToInt64(obj["Capacity"]);
  195. MemSize += mCap;
  196. }
  197. MemSize = (MemSize / 1024) / 1024;
  198. return MemSize.ToString() + "MB";
  199. }
  200. ///
  201. /// Retrieving No of Ram Slot on Motherboard.
  202. ///
  203. ///
  204. public static string GetNoRamSlots()
  205. {
  206. int MemSlots = 0;
  207. ManagementScope oMs = new ManagementScope();
  208. ObjectQuery oQuery2 = new ObjectQuery("SELECT MemoryDevices FROM Win32_PhysicalMemoryArray");
  209. ManagementObjectSearcher oSearcher2 = new ManagementObjectSearcher(oMs, oQuery2);
  210. ManagementObjectCollection oCollection2 = oSearcher2.Get();
  211. foreach (ManagementObject obj in oCollection2)
  212. {
  213. MemSlots = Convert.ToInt32(obj["MemoryDevices"]);
  214. }
  215. return MemSlots.ToString();
  216. }
  217. //Get CPU Temprature.
  218. ///
  219. /// method for retrieving the CPU Manufacturer
  220. /// using the WMI class
  221. ///
  222. /// CPU Manufacturer
  223. public static string GetCPUManufacturer()
  224. {
  225. string cpuMan = String.Empty;
  226. //create an instance of the Managemnet class with the
  227. //Win32_Processor class
  228. ManagementClass mgmt = new ManagementClass("Win32_Processor");
  229. //create a ManagementObjectCollection to loop through
  230. ManagementObjectCollection objCol = mgmt.GetInstances();
  231. //start our loop for all processors found
  232. foreach (ManagementObject obj in objCol)
  233. {
  234. if (cpuMan == String.Empty)
  235. {
  236. // only return manufacturer from first CPU
  237. cpuMan = obj.Properties["Manufacturer"].Value.ToString();
  238. }
  239. }
  240. return cpuMan;
  241. }
  242. ///
  243. /// method to retrieve the CPU's current
  244. /// clock speed using the WMI class
  245. ///
  246. /// Clock speed
  247. public static int GetCPUCurrentClockSpeed()
  248. {
  249. int cpuClockSpeed = 0;
  250. //create an instance of the Managemnet class with the
  251. //Win32_Processor class
  252. ManagementClass mgmt = new ManagementClass("Win32_Processor");
  253. //create a ManagementObjectCollection to loop through
  254. ManagementObjectCollection objCol = mgmt.GetInstances();
  255. //start our loop for all processors found
  256. foreach (ManagementObject obj in objCol)
  257. {
  258. if (cpuClockSpeed == 0)
  259. {
  260. // only return cpuStatus from first CPU
  261. cpuClockSpeed = Convert.ToInt32(obj.Properties["CurrentClockSpeed"].Value.ToString());
  262. }
  263. }
  264. //return the status
  265. return cpuClockSpeed;
  266. }
  267. ///
  268. /// method to retrieve the network adapters
  269. /// default IP gateway using WMI
  270. ///
  271. /// adapters default IP gateway
  272. public static string GetDefaultIPGateway()
  273. {
  274. //create out management class object using the
  275. //Win32_NetworkAdapterConfiguration class to get the attributes
  276. //of the network adapter
  277. ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
  278. //create our ManagementObjectCollection to get the attributes with
  279. ManagementObjectCollection objCol = mgmt.GetInstances();
  280. string gateway = String.Empty;
  281. //loop through all the objects we find
  282. foreach (ManagementObject obj in objCol)
  283. {
  284. if (gateway == String.Empty) // only return MAC Address from first card
  285. {
  286. //grab the value from the first network adapter we find
  287. //you can change the string to an array and get all
  288. //network adapters found as well
  289. //check to see if the adapter's IPEnabled
  290. //equals true
  291. if ((bool)obj["IPEnabled"] == true)
  292. {
  293. gateway = obj["DefaultIPGateway"].ToString();
  294. }
  295. }
  296. //dispose of our object
  297. obj.Dispose();
  298. }
  299. //replace the ":" with an empty space, this could also
  300. //be removed if you wish
  301. gateway = gateway.Replace(":", "");
  302. //return the mac address
  303. return gateway;
  304. }
  305. ///
  306. /// Retrieve CPU Speed.
  307. ///
  308. ///
  309. public static double? GetCpuSpeedInGHz()
  310. {
  311. double? GHz = null;
  312. using (ManagementClass mc = new ManagementClass("Win32_Processor"))
  313. {
  314. foreach (ManagementObject mo in mc.GetInstances())
  315. {
  316. GHz = 0.001 * (UInt32)mo.Properties["CurrentClockSpeed"].Value;
  317. break;
  318. }
  319. }
  320. return GHz;
  321. }
  322. ///
  323. /// Retrieving Current Language
  324. ///
  325. ///
  326. public static string GetCurrentLanguage()
  327. {
  328. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
  329. foreach (ManagementObject wmi in searcher.Get())
  330. {
  331. try
  332. {
  333. return wmi.GetPropertyValue("CurrentLanguage").ToString();
  334. }
  335. catch { }
  336. }
  337. return "BIOS Maker: Unknown";
  338. }
  339. ///
  340. /// Retrieving Current Language.
  341. ///
  342. ///
  343. public static string GetOSInformation()
  344. {
  345. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
  346. foreach (ManagementObject wmi in searcher.Get())
  347. {
  348. try
  349. {
  350. return ((string)wmi["Caption"]).Trim() + ", " + (string)wmi["Version"] + ", " + (string)wmi["OSArchitecture"];
  351. }
  352. catch { }
  353. }
  354. return "BIOS Maker: Unknown";
  355. }
  356. ///
  357. /// Retrieving Processor Information.
  358. ///
  359. ///
  360. public static String GetProcessorInformation()
  361. {
  362. ManagementClass mc = new ManagementClass("win32_processor");
  363. ManagementObjectCollection moc = mc.GetInstances();
  364. String info = String.Empty;
  365. foreach (ManagementObject mo in moc)
  366. {
  367. string name = (string)mo["Name"];
  368. name = name.Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©").Replace(" ", " ").Replace(" ", " ");
  369. info = name + ", " + (string)mo["Caption"] + ", " + (string)mo["SocketDesignation"];
  370. //mo.Properties["Name"].Value.ToString();
  371. //break;
  372. }
  373. return info;
  374. }
  375. ///
  376. /// Retrieving Computer Name.
  377. ///
  378. ///
  379. public static String GetComputerName()
  380. {
  381. ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
  382. ManagementObjectCollection moc = mc.GetInstances();
  383. String info = String.Empty;
  384. foreach (ManagementObject mo in moc)
  385. {
  386. info = (string)mo["Name"];
  387. //mo.Properties["Name"].Value.ToString();
  388. //break;
  389. }
  390. return info;
  391. }
  392. }

设计窗口形式

请参见下图设计您的 Windows 窗体并生成按钮单击事件以使用 C# 获取计算机硬件信息。

设计形式

添加代码隐藏源

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace HardwareInformation
  10. {
  11. public partial class HardwareInfo_CodeScratcher : Form
  12. {
  13. public HardwareInfo_CodeScratcher()
  14. {
  15. InitializeComponent();
  16. }
  17. private void HardwareInfo_CodeScratcher_Load(object sender, EventArgs e)
  18. {
  19. }
  20. private void ProcId_Click(object sender, EventArgs e)
  21. {
  22. lblPid.Text = HardwareInfo.GetProcessorId();
  23. }
  24. private void HDDSNo_Click(object sender, EventArgs e)
  25. {
  26. lblHDD.Text = HardwareInfo.GetHDDSerialNo();
  27. }
  28. private void BoardMake_Click(object sender, EventArgs e)
  29. {
  30. lblBM.Text = HardwareInfo.GetBoardMaker();
  31. }
  32. private void BIOSMkr_Click(object sender, EventArgs e)
  33. {
  34. lblBios.Text = HardwareInfo.GetBIOSmaker();
  35. }
  36. private void PhysicalMem_Click(object sender, EventArgs e)
  37. {
  38. lblPM.Text = HardwareInfo.GetPhysicalMemory();
  39. }
  40. private void CPUSpeed_Click(object sender, EventArgs e)
  41. {
  42. lblCS.Text = HardwareInfo.GetCpuSpeedInGHz().ToString();
  43. }
  44. private void CPUMkr_Click(object sender, EventArgs e)
  45. {
  46. lblCM.Text = HardwareInfo.GetCPUManufacturer();
  47. }
  48. }
  49. }

运行项目并检查最终输出

硬件信息 - 输出

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/sysmno/article/detail/61777
推荐阅读
相关标签
  

闽ICP备14008679号