C# 获取当前电脑CPU型号,显卡名称,数量,内存条数量,大小,硬盘大小
|
admin
2023年10月23日 16:0
本文热度 457
|
1. //获取CPU名称
2. private void button1_Click(object sender, EventArgs e)
3. {
4. string CPUName = "";
5. ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Processor");//Win32_Processor CPU处理器
6. foreach (ManagementObject mo in mos.Get())
7. {
8. CPUName = mo["Name"].ToString();
9. }
10. mos.Dispose();
11. label1.Text = CPUName;
12. }
13. //获取显卡数量,及显卡名称
14. private void button2_Click(object sender, EventArgs e)
15. {
16. string DisplayName = "";
17. ManagementClass m = new ManagementClass("Win32_VideoController");
18. ManagementObjectCollection mn = m.GetInstances();
19. DisplayName = "显卡数量:" + mn.Count.ToString() + " ";
20. ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_VideoController");//Win32_VideoController 显卡
21. int count = 0;
22. foreach (ManagementObject mo in mos.Get())
23. {
24. count++;
25. DisplayName += "第" + count.ToString() + "张显卡名称:" + mo["Name"].ToString() + " ";
26. }
27. mn.Dispose();
28. m.Dispose();
29. label2.Text = DisplayName;
30. }
31. //内存条数量,及大小
32. private void button3_Click(object sender, EventArgs e)
33. {
34. string PhysicalMemory = "";
35. ManagementClass m = new ManagementClass("Win32_PhysicalMemory");//内存条
36. ManagementObjectCollection mn = m.GetInstances();
37. PhysicalMemory = "物理内存条数量:" + mn.Count.ToString() + " ";
38. double capacity = 0.0;
39. int count = 0;
40. foreach (ManagementObject mo1 in mn)
41. {
42. count++;
43. capacity = ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
44. PhysicalMemory += "第" + count.ToString() + "张内存条大小:" + capacity.ToString() + "G ";
45. }
46. mn.Dispose();
47. m.Dispose();
48. label3.Text = PhysicalMemory;
49. }
50. //硬盘大小
51. private void button4_Click(object sender, EventArgs e)
52. {
53. string DiskDrive = "硬盘为:";
54. ManagementClass m = new ManagementClass("win32_DiskDrive");//硬盘
55. ManagementObjectCollection mn = m.GetInstances();
56. double capacity = 0.0;
57. foreach (ManagementObject mo1 in mn)
58. {
59. capacity += Int64.Parse(mo1.Properties["Size"].Value.ToString()) / 1024 / 1024/ 1024;
60. }
61. mn.Dispose();
62. m.Dispose();
63. label4.Text = DiskDrive + capacity.ToString();
64. }
该文章在 2023/10/23 16:01:59 编辑过