來源百度AI回答
1. Windows系統
在Windows系統上,你可以使用SystemInformation類(屬于System.Windows.Forms命名空間)來獲取一些基本的設備信息,例如制造商、型號等。但是,請注意,這種方法可能不會提供所有設備的詳細型號信息。
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); // 輸出將包含硬件信息。 } } }
浙公網安備 33010602011771號