Contents
  1. 1. Environment
  2. 2. ManagementClass

C#中有几个API可以很方便的查看电脑硬件和系统信息。

Environment

下面的示例程序展示了从中Environment我们能拿到哪些东西。比如

  1. OSVersion告诉我们操作系统的版本号。
  2. GetLogicalDrives可以告诉我们现在都有哪些盘符了。
  3. Is64BitOperatingSystem告诉我们是不是64位的系统。
  4. ProcessorCount告诉我们有几个CPU。
  5. GetEnvironmentVariables告诉我们现在的环境变量都有哪些,我们也可以只看进程的,用户的,机器的。
  6. GetFolderPath告诉我们特殊文件夹(SpecialFolder)的位置,包含DesktopFavoritesProgramFilesX86RecentSystemX86等。
    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
    29
    Console.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static void Main(string[] args)
{
OutputManagementClass("Win32_Processor");
OutputManagementClass("Win32_LogicalDisk");
}
private static void OutputManagementClass(string path)
{
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("From {0}", path);
foreach (var obj in new ManagementClass(path).GetInstances())
{
foreach (var myProperty in new ManagementClass(path).Properties)
{
Console.WriteLine("{0}: {1}", myProperty.Name, obj.Properties[myProperty.Name].Value);
}
}
Console.WriteLine();
}

磁盘方面,我们可以拿到关于磁盘总大小,剩余空间大小,文件系统等信息。

CPU方面,我们可以拿到主频,缓存大小,CPU物理核数和逻辑核数等信息。

Contents
  1. 1. Environment
  2. 2. ManagementClass