C# performs hardware class operations on Windows using ManagementObjectSearcher.

In C#, the ManagementObjectSearcher class can be used to operate on Windows hardware. This class is used to execute WMI queries and return a set of WMI objects that meet the query criteria.

Here is a sample code demonstrating how to use the ManagementObjectSearcher class to retrieve hardware information from a computer.

using System;
using System.Management;

public class HardwareInfo
{
    public static void Main()
    {
        // 创建WMI查询字符串
        string query = "SELECT * FROM Win32_Processor";

        // 创建ManagementObjectSearcher对象,用于执行WMI查询
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

        // 执行查询并获取查询结果
        ManagementObjectCollection queryResults = searcher.Get();

        // 遍历查询结果
        foreach (ManagementObject mo in queryResults)
        {
            // 获取ProcessorId属性的值
            string processorId = mo["ProcessorId"].ToString();

            // 输出ProcessorId属性的值
            Console.WriteLine("Processor ID: " + processorId);
        }
    }
}

In the example above, we first create a WMI query string, then use that query string to create a ManagementObjectSearcher object. Next, we call the Get method to execute the query and retrieve the results. Finally, we iterate through the results and output the value of the ProcessorId property.

In addition to the Win32_Processor class, there are many other WMI classes available for retrieving different types of hardware information, such as the Win32_LogicalDisk class for logical disk information, and the Win32_NetworkAdapter class for network adapter information. You can choose the appropriate WMI class for your specific needs.

bannerAds