C# 获取电脑的硬件所有信息

public class ComputerInfoHelper
{
    /// 
    /// 获取 CPU 所有信息
    /// 
    /// 
    public static Dictionary<string, object> GetCPUInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("制造商" + num, obj["Manufacturer"]);
                keyValues.Add("型号" + num, obj["Name"]);
                keyValues.Add("处理器ID" + num, obj["ProcessorId"]);
                keyValues.Add("核心数" + num, obj["NumberOfCores"]);
                keyValues.Add("线程数" + num, obj["NumberOfLogicalProcessors"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取 CPU 信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取硬盘所有信息
    /// 
    /// 
    public static Dictionary<string, object> GetHardDriveInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("制造商" + num, obj["Manufacturer"]);
                keyValues.Add("型号" + num, obj["Model"]);
                keyValues.Add("序列号" + num, obj["SerialNumber"]);
                keyValues.Add("容量" + num, obj["Size"] + " bytes");
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取硬盘信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取内存所有信息
    /// 
    /// 
    public static Dictionary<string, object> GetMemoryInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("制造商" + num, obj["Manufacturer"]);
                keyValues.Add("速度" + num, obj["Speed"] + " MHz");
                keyValues.Add("容量" + num, obj["Capacity"] + " bytes");
                keyValues.Add("型号" + num, obj["PartNumber"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取内存信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取主板所有信息
    /// 
    /// 
    public static Dictionary<string, object> GetMotherboardInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("制造商"+ num, obj["Manufacturer"]);
                keyValues.Add("产品" + num, obj["Product"]);
                keyValues.Add("版本" + num, obj["Version"]);
                keyValues.Add("序列号" + num, obj["SerialNumber"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取主板信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取显卡信息
    /// 
    public static Dictionary<string,object> GetGraphicsCardInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("显卡名称" + num, obj["Caption"]);
                keyValues.Add("制造商" + num, obj["AdapterCompatibility"]);
                keyValues.Add("显存大小" + num, obj["AdapterRAM"] + " bytes");
                keyValues.Add("驱动版本" + num, obj["DriverVersion"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取显卡信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取声卡所有信息
    /// 
    public static Dictionary<string, object> GetSoundDeviceInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("设备名称" + num, obj["Caption"]);
                keyValues.Add("制造商" + num, obj["Manufacturer"]);
                keyValues.Add("设备ID" + num, obj["DeviceID"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取声卡信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取网卡所有信息
    /// 
    public static Dictionary<string, object> GetNetworkAdapterInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("适配器名称" + num, obj["Name"]);
                keyValues.Add("制造商" + num, obj["Manufacturer"]);
                keyValues.Add("适配器类型" + num, obj["AdapterType"]);
                keyValues.Add("MAC 地址" + num, obj["MACAddress"]);
                keyValues.Add("是否启用" + num, obj["NetEnabled"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取网卡信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取键盘信息
    /// 
    public static Dictionary<string, object> GetKeyboardInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Keyboard");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("设备名称" + num, obj["Description"]);
                keyValues.Add("制造商" + num, obj["Manufacturer"]);
                keyValues.Add("设备ID" + num, obj["DeviceID"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取键盘信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取鼠标信息
    /// 
    public static Dictionary<string, object> GetMouseInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PointingDevice");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("设备名称" + num, obj["Description"]);
                keyValues.Add("制造商" + num, obj["Manufacturer"]);
                keyValues.Add("设备ID" + num, obj["DeviceID"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取键盘信息时出现异常", ex.Message);
        }
        return keyValues;
    }
    /// 
    /// 获取显示器信息
    /// 
    public static Dictionary<string, object> GetMonitorInfo()
    {
        Dictionary<string, object> keyValues = new Dictionary<string, object>();
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
            ManagementObjectCollection collection = searcher.Get();

            int num = 1;
            foreach (ManagementObject obj in collection)
            {
                keyValues.Add("设备名称" + num, obj["Description"]);
                keyValues.Add("制造商" + num, obj["MonitorManufacturer"]);
                keyValues.Add("设备ID" + num, obj["DeviceID"]);
                num++;
            }
        }
        catch (Exception ex)
        {
            keyValues.Clear();
            keyValues.Add("获取显示器信息时出现异常", ex.Message);
        }
        return keyValues;
    }
}

2023/11/22

你可能感兴趣的:(C#,c#,电脑,开发语言)