C#硬件信息

时间:2018-05-20 07:03:51

标签: c# hardware

我有这个代码来获取硬件信息,我有两个问题:

  1. 我无法向我展示ram记忆和名字2

  2. 我需要对第二部分进行一些解释,以便了解它的工作原理。

  3. 代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Management;
    
       namespace ConsoleApp1
     {
      class Program
       {
        static void Main(string[] args)
        {
            Console.Write("Motherboard Manufacturer: ");
            GetComponent("Win32_BaseBoard", "Manufacturer");
            Console.Write("Motherboard Model:");
            GetComponent("Win32_BaseBoard", "Product");
            Console.Write("CPU:");
            GetComponent("Win32_Processor", "Name");
            Console.Write("GPUs:");
            GetComponent("Win32_VideoController", "Name");
            Console.Write("BIOS Brand: ");
            GetComponent("Win32_BIOS", "Manufacturer");
            Console.Write("BIOS version: ");
            GetComponent("Win32_BIOS", "Name");
            Console.Write("Audio:");
            GetComponent("Win32_SoundDevice", "ProductName");
            Console.Write("Optical Drives:");
            GetComponent("Win32_CDROMDrive", "Name");
            Console.Write("Device Name:");
            GetComponent("Win32_ComputerSystem", "Name");
            Console.Write("HDD:");
            GetComponent("Win32_DiskDrive", "Model");
            Console.Write("Network:");
            GetComponent("Win32_NetworkAdapter", "Name");
            Console.Read();
    
    
        }
    
    
    
        private static void GetComponent(string hwclass, string syntax)
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwclass);
            foreach (ManagementObject mj in mos.Get())
            {
                if (Convert.ToString(mj[syntax]) != "")
                    Console.WriteLine(Convert.ToString(mj[syntax]));
            }
        }
    }
    }
    

1 个答案:

答案 0 :(得分:0)

这是部分答案。

  1. 对于RAM(物理内存)使用以下类。

    Win32_PhysicalMemory
    
  2. 另外我建议使用而不是Name尝试读取Capicity,PartNumber或序列号属性,这样会有所帮助。 (标签也很有用)。