个性化阅读
专注于IT技术分析

如何使用C#在Winforms中检索基本和高级硬件和软件信息(GPU,硬盘,处理器,操作系统,打印机)

本文概述

除了需要有关系统的硬件和软件信息外, 对于初学者来说, 了解如何列出操作系统的硬件组件始终是一个很好的练习, 因为这有助于你了解有关编程的更多信息或仅对机器的清单进行清点。硬件。

在本文中, 你将学习如何在Winforms中使用C#检索系统的基本和高级信息。

重要的提示

在某些示例中, 你需要在项目中添加以下参考, 以检索有关系统的信息:

using System.Management;

但是, 在某些Visual Studio版本中(特别是在2010年和更高版本中), 你还需要在项目中手动添加引用(.DLL)。为此, 请按照下列步骤操作:

  1. 右键单击项目, 添加引用

  2. 选择”程序集(框架)”选项卡, 然后搜索System.Management, 最后添加引用, 然后单击”确定”。

手动添加System.Management参考Visual Studio

我们需要添加System.Management来在WMI类中创建查询。在此处阅读有关在msdn中检索.NET中的WMI类的更多信息。

另一方面, 请记住, 所有使用ManagementObjectSearcher类来获取系统信息的实现, 其属性值为整数值(0-100), 并且这些值与属性名称无关(例如, 使用Video_Controller GPU类)返回0到9之间的值的Architecture属性), 并且你期望一个非常特定的值(例如x86或x64), 则可能是你传递了一些信息!请阅读Microsoft开发人员网络网站上的类的文档(分别在文章的每个部分中提供), 以获取每个属性的详细说明。

例如, 如前所述, Architecture属性返回一个整数(对于你来说, 这对值没有意义), 但是此值指定下表的索引(而不是值本身):

编号
0 x86
1 MIPS
2 Α
3 PowerPC
5
6 ia64(基于Itanium的系统)
9 x64

因此由你决定如何根据msdn网站上的信息在应用程序中呈现信息。

现在你知道了, 让我们开始吧!

显卡(GPU)

要获取有关GPU的信息, 我们需要使用ManagementObjectSearcher类对Win32_VideoController类创建系统查询。

首先向你的班级添加以下参考:

using System.Management;

如果在代码中使用该类时仍然看到错误, 请阅读本文开头的”重要说明”。

如果一切正确, 那么你将能够查询Win32_VideoController类。 Win32_VideoController WMI类表示运行Windows的计算机系统上视频控制器的功能和管理能力。

要访问WMI类, 我们需要如前所述, 创建一个查询。可以使用ManagementObjectSearcher类(在System.Management中提供)创建此查询。它根据指定的查询检索管理对象的集合。此类是检索管理信息的更常用入口点之一。

请参阅ManagementObjectSearcher的以下实现, 以查询Win32_VideoController类以检索有关GPU的高级信息:

ManagementObjectSearcher myVideoObject = new ManagementObjectSearcher("select * from Win32_VideoController");

foreach (ManagementObject obj in myVideoObject.Get())
{
    Console.WriteLine("Name  -  " + obj["Name"]);
    Console.WriteLine("Status  -  " + obj["Status"]);
    Console.WriteLine("Caption  -  " + obj["Caption"]);
    Console.WriteLine("DeviceID  -  " + obj["DeviceID"]);
    Console.WriteLine("AdapterRAM  -  " + obj["AdapterRAM"]);
    Console.WriteLine("AdapterDACType  -  " + obj["AdapterDACType"]);
    Console.WriteLine("Monochrome  -  " + obj["Monochrome"]);
    Console.WriteLine("InstalledDisplayDrivers  -  " + obj["InstalledDisplayDrivers"]);
    Console.WriteLine("DriverVersion  -  " + obj["DriverVersion"]);
    Console.WriteLine("VideoProcessor  -  " + obj["VideoProcessor"]);
    Console.WriteLine("VideoArchitecture  -  " + obj["VideoArchitecture"]);
    Console.WriteLine("VideoMemoryType  -  " + obj["VideoMemoryType"]);
}

Win32_VideoController的ManagementObject具有更多属性, 你可以在此处阅读有关此类的更多信息以及Microsoft开发人员网络文档中的所有可用属性。

注意:请记住, 与Windows显示驱动程序模型(WDDM)不兼容的硬件对于此类的实例返回的属性值不准确, 或者仅返回空值。

输出应类似于以下内容:

/** 
Name  -  NVIDIA GeForce GTX 760
Status  -  OK
Caption  -  NVIDIA GeForce GTX 760
DeviceID  -  VideoController1
AdapterRAM  -  2147483648
AdapterDACType  -  Integrated RAMDAC
Monochrome  -  False
InstalledDisplayDrivers  -  nvd3dumx.dll, nvwgf2umx.dll, nvwgf2umx.dll, nvwgf2umx.dll, nvd3dum, nvwgf2um, nvwgf2um, nvwgf2um
DriverVersion  -  21.21.13.7306
VideoProcessor  -  GeForce GTX 760
VideoArchitecture  -  5
VideoMemoryType  -  2
**/

此外, 如果要将AdapterRAM值转换为人类可读的值(MB, GB)而不是字节, 还可以在”硬盘”区域中使用提到的SizeSuffix方法:

// Convert the string obj["AdapterRAM"] into a Long value
Console.WriteLine("AdapterRAM  -  " + SizeSuffix((long)Convert.ToDouble(obj["AdapterRAM"])));
// Outputs : AdapterRAM  -  2, 0 GB

硬盘驱动器(和其他类型的驱动器)

为了获得有关台式机上已安装驱动器的信息, 我们将依赖于System.IO名称空间中包含的DriveInfo类。此类提供对驱动器上信息的访问权限, 对驱动器进行建模, 并提供查询驱动器信息的方法和属性。使用DriveInfo可以确定哪些驱动器可用以及它们的驱动器类型。如果你的课程尚未被引用, 则需要向其添加以下引用:

using System;
using System.IO;

你可以使用它来查询以确定驱动器上的容量和可用空间。请参见以下示例:

DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
    Console.WriteLine("Drive {0}", d.Name);
    Console.WriteLine("  Drive type: {0}", d.DriveType);
    if (d.IsReady == true)
    {
        Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
        Console.WriteLine("  File system: {0}", d.DriveFormat);
        Console.WriteLine("  Available space to current user:{0, 15} bytes", d.AvailableFreeSpace);

        Console.WriteLine("  Total available space:          {0, 15} bytes", d.TotalFreeSpace);

        Console.WriteLine("  Total size of drive:            {0, 15} bytes ", d.TotalSize);
        Console.WriteLine("  Root directory:            {0, 12}", d.RootDirectory);
    }
}

allDrives变量将是一个可迭代的数组, 其中每个项目都包含有关系统中一个可用磁盘的信息(该磁盘可以是可移动[USB, USB硬盘], 固定[硬盘]和CDRom驱动器)。由于并非每个显示的磁盘都包含信息(对于CDRom, 当其中没有CD时), 你需要使用if语句drive.IsReady属性检查驱动器是否可用。前面的代码将在Output控制台中产生以下输出:

/**
Output of the information about the drives available in the system

Drive C:\
  Drive type: Fixed
  Volume label: 
  File system: NTFS
  Available space to current user:  1835493908480 bytes
  Total available space:            1835493908480 bytes
  Total size of drive:              1999804297216 bytes 
  Root directory:                     C:\
Drive D:\
  Drive type: CDRom
Drive E:\
  Drive type: Fixed
  Volume label: 
  File system: NTFS
  Available space to current user:   164813348864 bytes
  Total available space:             164813348864 bytes
  Total size of drive:               500096991232 bytes 
  Root directory:                     E:\
Drive F:\
  Drive type: Fixed
  Volume label: Carlos
  File system: NTFS
  Available space to current user:    39133581312 bytes
  Total available space:              39133581312 bytes
  Total size of drive:               320059994112 bytes 
  Root directory:                     F:\
Drive G:\
  Drive type: Removable
  Volume label: UBUNTU 16_1
  File system: FAT32
  Available space to current user:    30001135616 bytes
  Total available space:              30001135616 bytes
  Total size of drive:                31600672768 bytes 
  Root directory:                     G:\
**/

如你所见, 关于驱动器的可用空间和容量, 该值将以字节为单位返回。你可以使用以下实现(函数)以更易于理解的值(MB, GB, TB)检索短输出。该值将自动转换为最合理的值(如果可用兆字节, 则该值将以兆字节显示):

static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };

static string SizeSuffix(Int64 value)
{
    if (value < 0) { return "-" + SizeSuffix(-value); }
    if (value == 0) { return "0.0 bytes"; }

    int mag = (int)Math.Log(value, 1024);
    decimal adjustedSize = (decimal)value / (1L << (mag * 10));

    return string.Format("{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
}

要将长字节值转换为人类可读的值, 只需在返回数字值的值中使用SizeSuffix函数:

Console.WriteLine("  Available space to current user:{0, 15}", SizeSuffix(d.AvailableFreeSpace));
Console.WriteLine("  Total available space:          {0, 15}", SizeSuffix(d.TotalFreeSpace));
Console.WriteLine("  Total size of drive:            {0, 15} ", SizeSuffix(d.TotalSize));

现在, 所生成的输出值应该以使用Windows的方式更具可读性:

/**
Output of the information about the drives available in the system with readable values

Drive C:\
  Drive type: Fixed
  Volume label: 
  File system: NTFS
  Available space to current user:         1, 7 TB
  Total available space:                   1, 7 TB
  Total size of drive:                     1, 8 TB
  Root directory:                     C:\
Drive D:\
  Drive type: CDRom
Drive E:\
  Drive type: Fixed
  Volume label: 
  File system: NTFS
  Available space to current user:       153, 5 GB
  Total available space:                 153, 5 GB
  Total size of drive:                   465, 8 GB
  Root directory:                     E:\
Drive F:\
  Drive type: Fixed
  Volume label: Carlos
  File system: NTFS
  Available space to current user:        36, 4 GB
  Total available space:                  36, 4 GB
  Total size of drive:                   298, 1 GB
  Root directory:                     F:\
Drive G:\
  Drive type: Removable
  Volume label: UBUNTU 16_1
  File system: FAT32
  Available space to current user:        27, 9 GB
  Total available space:                  27, 9 GB
  Total size of drive:                    29, 4 GB
  Root directory:                     G:\
**/

处理器

要获取有关CPU的信息, 我们需要使用ManagementObjectSearcher类对Win32_Processor类创建系统查询。

首先向你的班级添加以下参考:

using System.Management;

如果在代码中使用该类时仍然看到错误, 请阅读本文开头的”重要说明”。

如果一切正确, 那么你将能够查询Win32_Processor类。 Win32_Processor WMI类表示可以解释在Windows操作系统上运行的计算机上的指令序列的设备。

要访问WMI类, 我们需要如前所述, 创建一个查询。可以使用ManagementObjectSearcher类(在System.Management中提供)创建此查询。

请参阅ManagementObjectSearcher的以下实现, 以查询Win32_Processor类以检索有关CPU的高级信息:

ManagementObjectSearcher myProcessorObject = new ManagementObjectSearcher("select * from Win32_Processor");

foreach (ManagementObject obj in myProcessorObject.Get())
{
    Console.WriteLine("Name  -  " + obj["Name"]);
    Console.WriteLine("DeviceID  -  " + obj["DeviceID"]);
    Console.WriteLine("Manufacturer  -  " + obj["Manufacturer"]);
    Console.WriteLine("CurrentClockSpeed  -  " + obj["CurrentClockSpeed"]);
    Console.WriteLine("Caption  -  " + obj["Caption"]);
    Console.WriteLine("NumberOfCores  -  " + obj["NumberOfCores"]);
    Console.WriteLine("NumberOfEnabledCore  -  " + obj["NumberOfEnabledCore"]);
    Console.WriteLine("NumberOfLogicalProcessors  -  " + obj["NumberOfLogicalProcessors"]);
    Console.WriteLine("Architecture  -  " + obj["Architecture"]);
    Console.WriteLine("Family  -  " + obj["Family"]);
    Console.WriteLine("ProcessorType  -  " + obj["ProcessorType"]);
    Console.WriteLine("Characteristics  -  " + obj["Characteristics"]);
    Console.WriteLine("AddressWidth  -  " + obj["AddressWidth"]);
}

Win32_Processor的ManagementObject具有更多属性, 你可以在此处阅读有关此类的更多信息以及Microsoft开发人员网络文档中的所有可用属性。

输出应类似于以下内容:

/**
Name  -  Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
DeviceID  -  CPU0
Manufacturer  -  GenuineIntel
CurrentClockSpeed  -  3300
Caption  -  Intel64 Family 6 Model 60 Stepping 3
NumberOfCores  -  4
NumberOfEnabledCore  -  4
NumberOfLogicalProcessors  -  4
Architecture  -  9
Family  -  205
ProcessorType  -  3
Characteristics  -  4
AddressWidth  -  64
*/

操作系统

要获取有关Operative系统的信息, 我们需要使用ManagementObjectSearcher类对Win32_OperatingSystem类创建系统查询。

首先向你的班级添加以下参考:

using System.Management;

如果在代码中使用该类时仍然看到错误, 请阅读本文开头的”重要说明”。

如果一切正确, 那么你将能够查询Win32_OperatingSystem类。 Win32_OperatingSystem WMI类表示计算机上安装的基于Windows的操作系统。

要访问WMI类, 我们需要如前所述, 创建一个查询。可以使用ManagementObjectSearcher类(在System.Management中提供)创建此查询。

请参阅下面的ManagementObjectSearcher实现, 以查询Win32_OperatingSystem类以检索有关Operative System的高级信息:

ManagementObjectSearcher myOperativeSystemObject = new ManagementObjectSearcher("select * from Win32_OperatingSystem");

foreach (ManagementObject obj in myOperativeSystemObject.Get())
{
    Console.WriteLine("Caption  -  " + obj["Caption"]);
    Console.WriteLine("WindowsDirectory  -  " + obj["WindowsDirectory"]);
    Console.WriteLine("ProductType  -  " + obj["ProductType"]);
    Console.WriteLine("SerialNumber  -  " + obj["SerialNumber"]);
    Console.WriteLine("SystemDirectory  -  " + obj["SystemDirectory"]);
    Console.WriteLine("CountryCode  -  " + obj["CountryCode"]);
    Console.WriteLine("CurrentTimeZone  -  " + obj["CurrentTimeZone"]);
    Console.WriteLine("EncryptionLevel  -  " + obj["EncryptionLevel"]);
    Console.WriteLine("OSType  -  " + obj["OSType"]);
    Console.WriteLine("Version  -  " + obj["Version"]);
}

Win32_OperatingSystem的ManagementObject具有更多属性, 你可以在此处阅读有关此类的更多信息以及Microsoft开发人员网络文档中的所有可用属性。

输出应类似于以下内容:

/**
Caption  -  Microsoft Windows 10 Pro
WindowsDirectory  -  C:\Windows
ProductType  -  1
SerialNumber  -  00000-00000-00000-00000
SystemDirectory  -  C:\Windows\system32
CountryCode  -  49
CurrentTimeZone  -  60
EncryptionLevel  -  256
OSType  -  18
Version  -  10.0.10586
*/

网络接口

若要获取有关网络接口的信息, 可以使用System.Net.NetworkInformation命名空间中可用的NetworkInterface类。此类提供网络接口的配置和统计信息。

首先将以下引用添加到你的类中:

using System;
using System.Net.NetworkInformation;

并检查以下简单的实现, 其中列出了所有可用的网络接口以及每个对象的某些可用属性:

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

if (nics == null || nics.Length < 1)
{
    Console.WriteLine("  No network interfaces found.");
}else{
    foreach (NetworkInterface adapter in nics)
    {
        IPInterfaceProperties properties = adapter.GetIPProperties();
        Console.WriteLine();
        Console.WriteLine(adapter.Description);
        Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
        Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
        Console.WriteLine("  Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString());
        Console.WriteLine("  Operational status ...................... : {0}", adapter.OperationalStatus);
    }
}

你可以在此处阅读有关msdn中的NetworkInterface类的更多信息。前面的代码应生成类似的输出:

/**
Realtek PCIe GBE Family Controller
==================================
  Interface type .......................... : Ethernet
  Physical Address ........................ : XXXXXXXXXXXX
  Operational status ...................... : Up

TAP-Windows Adapter V9
======================
  Interface type .......................... : Ethernet
  Physical Address ........................ : XXXXXXXXXXXX
  Operational status ...................... : Down

Bluetooth Device (Personal Area Network)
========================================
  Interface type .......................... : Ethernet
  Physical Address ........................ : XXXXXXXXXXXX
  Operational status ...................... : Down

Software Loopback Interface 1
=============================
  Interface type .......................... : Loopback
  Physical Address ........................ : 
  Operational status ...................... : Up

Teredo Tunneling Pseudo-Interface
=================================
  Interface type .......................... : Tunnel
  Physical Address ........................ : 00000000000000E0
  Operational status ...................... : Up

Microsoft ISATAP Adapter
========================
  Interface type .......................... : Tunnel
  Physical Address ........................ : 00000000000000E0
  Operational status ...................... : Down
*/

但是, 你可以使用NetworkInterfaces和IPGlobalProperty创建更详细的示例, 以检查网络接口是否支持IPv4和IPv6。如果可用, 它也会列出可用的蓝牙网络:

public static void ShowNetworkInterfaces()
{
    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    Console.WriteLine("Interface information for {0}.{1}     ", computerProperties.HostName, computerProperties.DomainName);
    if (nics == null || nics.Length < 1)
    {
        Console.WriteLine("  No network interfaces found.");
        return;
    }

    Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);

    foreach (NetworkInterface adapter in nics)
    {
        IPInterfaceProperties properties = adapter.GetIPProperties();
        Console.WriteLine();
        Console.WriteLine(adapter.Description);
        Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
        Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
        Console.WriteLine("  Physical Address ........................ : {0}", adapter.GetPhysicalAddress().ToString());
        Console.WriteLine("  Operational status ...................... : {0}", adapter.OperationalStatus);
        string versions = "";

        // Create a display string for the supported IP versions.
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
            versions = "IPv4";
        }
        if (adapter.Supports(NetworkInterfaceComponent.IPv6))
        {
            if (versions.Length > 0)
            {
                versions += " ";
            }
            versions += "IPv6";
        }
        Console.WriteLine("  IP version .............................. : {0}", versions);
        //ShowIPAddresses(properties);

        // The following information is not useful for loopback adapters.
        if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
        {
            continue;
        }
        Console.WriteLine("  DNS suffix .............................. : {0}", properties.DnsSuffix);

        string label;
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
            IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
            Console.WriteLine("  MTU...................................... : {0}", ipv4.Mtu);
            if (ipv4.UsesWins)
            {

                IPAddressCollection winsServers = properties.WinsServersAddresses;
                if (winsServers.Count > 0)
                {
                    label = "  WINS Servers ............................ :";
                    //ShowIPAddresses(label, winsServers);
                }
            }
        }

        Console.WriteLine("  DNS enabled ............................. : {0}", properties.IsDnsEnabled);
        Console.WriteLine("  Dynamically configured DNS .............. : {0}", properties.IsDynamicDnsEnabled);
        Console.WriteLine("  Receive Only ............................ : {0}", adapter.IsReceiveOnly);
        Console.WriteLine("  Multicast ............................... : {0}", adapter.SupportsMulticast);
    }
}

ShowNetwork的执行应生成类似于以下内容的输出:

/**
Interface information for **MyComputerName**.     
  Number of interfaces .................... : 6

Realtek PCIe GBE Family Controller
==================================
  Interface type .......................... : Ethernet
  Physical Address ........................ : XXXXXXXXXXXX
  Operational status ...................... : Up
  IP version .............................. : IPv4 IPv6
  DNS suffix .............................. : fritz.box
  MTU...................................... : 1500
  DNS enabled ............................. : False
  Dynamically configured DNS .............. : True
  Receive Only ............................ : False
  Multicast ............................... : True

TAP-Windows Adapter V9
======================
  Interface type .......................... : Ethernet
  Physical Address ........................ : XXXXXXXXXXXX
  Operational status ...................... : Down
  IP version .............................. : IPv4 IPv6
  DNS suffix .............................. : 
  MTU...................................... : 1500
  DNS enabled ............................. : False
  Dynamically configured DNS .............. : True
  Receive Only ............................ : False
  Multicast ............................... : True

Bluetooth Device (Personal Area Network)
========================================
  Interface type .......................... : Ethernet
  Physical Address ........................ : XXXXXXXXXXXX
  Operational status ...................... : Down
  IP version .............................. : IPv4 IPv6
  DNS suffix .............................. : 
  MTU...................................... : 1500
  DNS enabled ............................. : False
  Dynamically configured DNS .............. : True
  Receive Only ............................ : False
  Multicast ............................... : True

Software Loopback Interface 1
=============================
  Interface type .......................... : Loopback
  Physical Address ........................ : 
  Operational status ...................... : Up
  IP version .............................. : IPv4 IPv6

Teredo Tunneling Pseudo-Interface
=================================
  Interface type .......................... : Tunnel
  Physical Address ........................ : 00000000000000E0
  Operational status ...................... : Up
  IP version .............................. : IPv6
  DNS suffix .............................. : 
  DNS enabled ............................. : False
  Dynamically configured DNS .............. : False
  Receive Only ............................ : False
  Multicast ............................... : False

Microsoft ISATAP Adapter
========================
  Interface type .......................... : Tunnel
  Physical Address ........................ : 00000000000000E0
  Operational status ...................... : Down
  IP version .............................. : IPv6
  DNS suffix .............................. : fritz.box
  DNS enabled ............................. : False
  Dynamically configured DNS .............. : True
  Receive Only ............................ : False
  Multicast ............................... : False
*/

声卡(音频设备)

要获取有关音频设备的信息, 我们需要使用ManagementObjectSearcher类对Win32_SoundDevice类创建系统查询。

首先向你的班级添加以下参考:

using System.Management;

如果在代码中使用该类时仍然看到错误, 请阅读本文开头的”重要说明”。

如果一切正确, 那么你将能够查询Win32_SoundDevice类。 Win32_SoundDevice WMI类表示运行Windows的计算机系统上声音设备的属性。

要访问WMI类, 我们需要如前所述, 创建一个查询。可以使用ManagementObjectSearcher类(在System.Management中提供)创建此查询。

以下代码段将列出所有音频设备(麦克风, 扬声器, 声卡等)及其属性:

ManagementObjectSearcher myAudioObject = new ManagementObjectSearcher("select * from Win32_SoundDevice");

foreach (ManagementObject obj in myAudioObject.Get())
{
    Console.WriteLine("Name  -  " + obj["Name"]);
    Console.WriteLine("ProductName  -  " + obj["ProductName"]);
    Console.WriteLine("Availability  -  " + obj["Availability"]);
    Console.WriteLine("DeviceID  -  " + obj["DeviceID"]);
    Console.WriteLine("PowerManagementSupported  -  " + obj["PowerManagementSupported"]);
    Console.WriteLine("Status  -  " + obj["Status"]);
    Console.WriteLine("StatusInfo  -  " + obj["StatusInfo"]);
    Console.WriteLine(String.Empty.PadLeft(obj["ProductName"].ToString().Length, '='));
}

Win32_SoundDevice的ManagementObject具有更多属性, 你可以在此处阅读有关此类的更多信息以及Microsoft开发人员网络文档中的所有可用属性。

上一个代码段的输出应类似于以下内容:

/**
Name  -  Logitech Mic (QuickCam S5500)
ProductName  -  Logitech Mic (QuickCam S5500)
Availability  -  
DeviceID  -  USB\VID_046D&PID_09A1&MI_02\8&1DEC173F&0&0002
PowerManagementSupported  -  False
Status  -  OK
StatusInfo  -  3
=============================
Name  -  NVIDIA High Definition Audio
ProductName  -  NVIDIA High Definition Audio
Availability  -  
DeviceID  -  HDAUDIO\FUNC_01&VEN_10DE&DEV_0040&SUBSYS_1043847A&REV_1001\5&2F2C87F6&0&0001
PowerManagementSupported  -  False
Status  -  OK
StatusInfo  -  3
============================
Name  -  High Definition Audio-Gerät
ProductName  -  High Definition Audio-Gerät
Availability  -  
DeviceID  -  HDAUDIO\FUNC_01&VEN_10EC&DEV_0892&SUBSYS_1462D816&REV_1003\4&C65B857&0&0001
PowerManagementSupported  -  False
Status  -  OK
StatusInfo  -  3
===========================
Name  -  Creative X-Fi Audio Processor (WDM)
ProductName  -  Creative X-Fi Audio Processor (WDM)
Availability  -  
DeviceID  -  PCI\VEN_1102&DEV_000B&SUBSYS_00431102&REV_04\4&2F87A1B6&0&00E4
PowerManagementSupported  -  False
Status  -  OK
StatusInfo  -  3
===================================
Name  -  NVIDIA Virtual Audio Device (Wave Extensible) (WDM)
ProductName  -  NVIDIA Virtual Audio Device (Wave Extensible) (WDM)
Availability  -  
DeviceID  -  ROOT\UNNAMED_DEVICE\0000
PowerManagementSupported  -  False
Status  -  OK
StatusInfo  -  3
===================================================
*/

印表机

要获取有关音频设备的信息, 我们需要使用ManagementObjectSearcher类对Win32_Printer类创建系统查询。

首先向你的班级添加以下参考:

using System.Management;

如果在代码中使用该类时仍然看到错误, 请阅读本文开头的”重要说明”。

如果一切正确, 那么你将能够查询Win32_Printer类。 Win32_Printer WMI类表示连接到在Microsoft Windows操作系统上运行的计算机的设备, 该设备可以在纸张或其他介质上产生打印的图像或文本。

要访问WMI类, 我们需要如前所述, 创建一个查询。可以使用ManagementObjectSearcher类(在System.Management中提供)创建此查询。

以下代码段列出了系统中所有可用的打印机(甚至包括Microsoft Print to PDF服务):

ManagementObjectSearcher myPrinterObject = new ManagementObjectSearcher("select * from Win32_Printer");

foreach (ManagementObject obj in myPrinterObject.Get())
{
    Console.WriteLine("Name  -  " + obj["Name"]);
    Console.WriteLine("Network  -  " + obj["Network"]);
    Console.WriteLine("Availability  -  " + obj["Availability"]);
    Console.WriteLine("Is default printer  -  " + obj["Default"]);
    Console.WriteLine("DeviceID  -  " + obj["DeviceID"]);
    Console.WriteLine("Status  -  " + obj["Status"]);
    
    Console.WriteLine(String.Empty.PadLeft(obj["Name"].ToString().Length, '='));
}

Win32_Printer的ManagementObject具有更多属性, 你可以在此处阅读有关此类的更多信息以及microsoft开发人员网络文档中的所有可用属性。

输出应类似于:

/**
Name  -  Microsoft XPS Document Writer
Network  -  False
Availability  -  
Is default printer  -  False
DeviceID  -  Microsoft XPS Document Writer
Status  -  Unknown
=============================
Name  -  Microsoft Print to PDF
Network  -  False
Availability  -  
Is default printer  -  False
DeviceID  -  Microsoft Print to PDF
Status  -  Unknown
======================
Name  -  Fax
Network  -  False
Availability  -  
Is default printer  -  False
DeviceID  -  Fax
Status  -  Unknown
===
Name  -  Enviar a OneNote 2013
Network  -  False
Availability  -  
Is default printer  -  False
DeviceID  -  Enviar a OneNote 2013
Status  -  Unknown
=====================
Name  -  Brother HL-3070CW series
Network  -  False
Availability  -  
Is default printer  -  True
DeviceID  -  Brother HL-3070CW series
Status  -  Unknown
========================
**/

玩得开心 !

赞(0)
未经允许不得转载:srcmini » 如何使用C#在Winforms中检索基本和高级硬件和软件信息(GPU,硬盘,处理器,操作系统,打印机)

评论 抢沙发

评论前必须登录!