用C#来查看电脑硬件和系统信息
Contents
C#中有几个API可以很方便的查看电脑硬件和系统信息。
Environment
下面的示例程序展示了从中Environment
我们能拿到哪些东西。比如
OSVersion
告诉我们操作系统的版本号。GetLogicalDrives
可以告诉我们现在都有哪些盘符了。Is64BitOperatingSystem
告诉我们是不是64位的系统。ProcessorCount
告诉我们有几个CPU。GetEnvironmentVariables
告诉我们现在的环境变量都有哪些,我们也可以只看进程的,用户的,机器的。GetFolderPath
告诉我们特殊文件夹(SpecialFolder)的位置,包含Desktop
、Favorites
、ProgramFilesX86
、Recent
、SystemX86
等。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29Console.WriteLine("------------------------------------------------------");
Console.WriteLine("From Environment:");
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("CommandLine: " + Environment.CommandLine);
Console.WriteLine("CurrentDirectory: " + Environment.CurrentDirectory);
Console.WriteLine("CurrentManagedThreadId: " + Environment.CurrentManagedThreadId);
Console.WriteLine("ExitCode: " + Environment.ExitCode);
Console.WriteLine("LogicalDrives: " + string.Join(",", Environment.GetLogicalDrives()));
Console.WriteLine("HasShutdownStarted: " + Environment.HasShutdownStarted);
Console.WriteLine("Is64BitOperatingSystem: " + Environment.Is64BitOperatingSystem);
Console.WriteLine("Is64BitProcess: " + Environment.Is64BitProcess);
Console.WriteLine("MachineName: " + Environment.MachineName);
Console.WriteLine("OSVersion: " + Environment.OSVersion);
Console.WriteLine("ProcessorCount: " + Environment.ProcessorCount);
Console.WriteLine("StackTrace: " + Environment.StackTrace);
Console.WriteLine("SystemDirectory: " + Environment.SystemDirectory);
Console.WriteLine("SystemPageSize: " + Environment.SystemPageSize);
Console.WriteLine("TickCount: " + Environment.TickCount);
Console.WriteLine("UserDomainName: " + Environment.UserDomainName);
Console.WriteLine("UserInteractive: " + Environment.UserInteractive);
Console.WriteLine("UserName: " + Environment.UserName);
Console.WriteLine("Version: " + Environment.Version);
Console.WriteLine("WorkingSet: " + Environment.WorkingSet);
Console.WriteLine("Desktop: " + Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
Console.WriteLine("EnvironmentVariables:");
foreach (DictionaryEntry envVar in Environment.GetEnvironmentVariables())
{
Console.WriteLine("{0} : {1}", envVar.Key, envVar.Value);
}ManagementClass
ManagementClass
可以告诉我们关于逻辑磁盘和CPU的更详细的信息,需要添加reference到System.Management
。示例代码如下:
1 | private static void Main(string[] args) |
磁盘方面,我们可以拿到关于磁盘总大小,剩余空间大小,文件系统等信息。
CPU方面,我们可以拿到主频,缓存大小,CPU物理核数和逻辑核数等信息。