来源百度AI回答
1. Windows系统
在Windows系统上,你可以使用SystemInformation类(属于System.Windows.Forms命名空间)来获取一些基本的设备信息,例如制造商、型号等。但是,请注意,这种方法可能不会提供所有设备的详细型号信息。
using System;
using System.Windows.Forms;
 
class Program
{
    static void Main()
    {
        string manufacturer = SystemInformation.FriendlyOSName; // 例如 "Windows 10"
        string model = SystemInformation.OemProductKey; // OEM 产品密钥,在某些情况下可以用来推断型号
        Console.WriteLine($"制造商: {manufacturer}");
        Console.WriteLine($"型号: {model}");
    }
}

对于更详细的硬件信息,比如CPU、主板等,你可以使用ManagementObjectSearcher类(属于System.Management命名空间)来查询WMI(Windows Management Instrumentation)数据库。

using System;
using System.Management;
 
class Program
{
    static void Main()
    {
        string query = "SELECT * FROM Win32_ComputerSystem";
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        {
            foreach (ManagementObject mo in searcher.Get())
            {
                Console.WriteLine("制造商: " + mo["Manufacturer"]);
                Console.WriteLine("型号: " + mo["Model"]);
            }
        }
    }
}

2. macOS系统
在macOS上,你可以使用SystemInformation类(属于System.Runtime.InteropServices命名空间)来获取一些系统信息。但是,对于硬件详细信息,通常需要调用特定的命令行工具或API,如system_profiler。在C#中,你可以通过调用外部命令来实现这一点。

using System;
using System.Diagnostics;
 
class Program
{
    static void Main()
    {
        ProcessStartInfo processStartInfo = new ProcessStartInfo("system_profiler", "SPHardwareDataType");
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.UseShellExecute = false;
        processStartInfo.CreateNoWindow = true;
        using (Process process = Process.Start(processStartInfo))
        {
            string output = process.StandardOutput.ReadToEnd();
            Console.WriteLine(output); // 输出将包含硬件信息,例如型号和制造商。
        }
    }
}

3. Linux系统
在Linux系统上,你可以通过读取特定的文件或调用命令行工具来获取硬件信息。例如,使用lshw命令:

using System;
using System.Diagnostics;
 
class Program
{
    static void Main()
    {
        ProcessStartInfo processStartInfo = new ProcessStartInfo("lshw", "-short"); // 使用-short选项获取简洁输出
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.UseShellExecute = false;
        processStartInfo.CreateNoWindow = true;
        using (Process process = Process.Start(processStartInfo))
        {
            string output = process.StandardOutput.ReadToEnd();
            Console.WriteLine(output); // 输出将包含硬件信息。
        }
    }
}

 

posted on 2025-02-14 13:14  邢帅杰  阅读(8)  评论(0编辑  收藏  举报