简体   繁体   English

C#获取连接的设备信息

[英]C# get connected device information

I am looking for a way to get information about what peripherals currently are connected to the machine that is running the application. 我正在寻找一种获取有关当前哪些外围设备连接到运行该应用程序的计算机的信息的方法。

Peripherals kinds: 外围设备种类:

  • Displays (Resolution, Manufacturer, Model, Serialnumber and/or unique device identifier) 显示(分辨率,制造商,型号,序列号和/或唯一的设备标识符)
  • Printers (Manufacturer, Model, Serialnumber and/or unique device identifier) 打印机(制造商,型号,序列号和/或唯一的设备标识符)
  • Mouse (Manufacturer, Model, Serialnumber and/or unique device identifier) 鼠标(制造商,型号,序列号和/或唯一的设备标识符)
  • Keyboard (Manufacturer, Model, Serialnumber and/or unique device identifier) 键盘(制造商,型号,序列号和/或唯一的设备标识符)
  • etc 等等

I tried several WMI classes (win32_desktopmonitor, win32_pnpentity, win32_printer, etc) but until now, i haven't found a api or library that gives me all the searched data. 我尝试了几种WMI类(win32_desktopmonitor,win32_pnpentity,win32_printer等),但是直到现在,我还没有找到可以提供所有搜索数据的api或库。 They either don't have that data, return empty or return with a generic reference. 他们要么没有该数据,要么返回空或返回一个通用引用。

The primary purpose of this application is collection of device information. 此应用程序的主要目的是收集设备信息。

Has someone any example or can give me a direction in what library or api i need to look for those data. 有谁的例子或可以给我一个方向,我需要寻找那些数据的库或API。

For monitor information 有关监视器信息

select * from win32_pnpentity where PNPClass = 'Monitor'

There are 2 kinds of printers local or network. 本地或网络上有2种打印机。 For local printers, Win32_Printer will work. 对于本地打印机, Win32_Printer将起作用。

But for network printers you need to read registry key or run query. 但是对于网络打印机,您需要阅读注册表项或运行查询。 Registry key has very refined information. 注册表项具有非常完善的信息。

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers\Client Side Rendering Print Provider\Servers\<Printer_Name>\Printers\<Some ID>


select * from win32_pnpentity where PNPClass = 'Printer'

For keyboard and mouse information, whatever we are showing in Device Manager , same information will be there in Win32_PNPEntity 对于键盘和鼠标信息,无论我们在设备管理器中显示什么, Win32_PNPEntity都将包含相同的信息

select * from win32_pnpentity where PNPClass = 'Keyboard'

select * from win32_pnpentity where PNPClass = 'Mouse'

There are two ways to go about doing this... 有两种方法可以执行此操作...

Using System.Management 使用系统管理

This is a class available in .NET already and can be added as a reference to your project. 这是.NET中已经可用的类,可以添加为对您的项目的引用。 Once you've added it as a reference, remember to include using System.Management; 将其添加为参考后,请记住包括using System.Management; at the top of your code. 在代码的顶部。

ManagementObjectSearcher win32Monitor = new ManagementObjectSearcher("select * from Win32_DesktopMonitor");

foreach (ManagementObject obj in win32Monitor.Get())
{
    Console.WriteLine(obj["ScreenWidth"].ToString());
    Console.WriteLine(obj["ScreenHeight"].ToString());
    Console.WriteLine(obj["Manufacturer"].ToString());
    Console.WriteLine(obj["DeviceID"].ToString());
}

This will provide all of the data you require. 这将提供您需要的所有数据。 The DeviceID is a 'Unique identifier' as defined by MSDN documentation. DeviceID是MSDN文档定义的“唯一标识符”。 There are many more details you can access about each of the classes I reference here that I explain later. 您可以访问有关我在此处引用的每个类的更多详细信息,稍后将对其进行解释。

Although using System.Management is completely reasonable and gets all the data you need, I found it a bit clumsy in practice as it requires you to manually convert every attribute and write a SQL query to get your data, so I wrote my own library to tackle this problem called SimpleWMI . 尽管使用System.Management是完全合理的并且可以获取您需要的所有数据,但是在实践中我发现它有点笨拙,因为它需要您手动转换每个属性并编写SQL查询来获取数据,因此我编写了自己的库来解决这个称为SimpleWMI问题。

Using SimpleWMI 使用SimpleWMI

To install this library or understand what it can do in a bit more depth visit the GitHub page where you can find downloads etc. 要安装此库或进一步了解它可以做什么,请访问GitHub页面 ,您可以在其中找到下载内容等。

I have also implemented this exact situation in the example project on GitHub so that you know what to do. 我还在GitHub上的示例项目中实现了这种确切情况,以便您知道该怎么做。

This performs the same function as above, but the code is easier to remember and doesn't require conversion of data: 它执行与上面相同的功能,但是代码更容易记住,并且不需要数据转换:

foreach (dynamic obj in WMIQuery.GetAllObjects(Win32.PointingDevice))
{
    Console.WriteLine(obj.Name);
    Console.WriteLine(obj.Manufacturer);
    Console.WriteLine(obj.DeviceID);
}

To query another class, just replace PointingDevice with whatever over Win32 class you wish (eg DesktopMonitor, Keyboard or Printer). 要查询另一个类,只需将PointingDevice替换为所需的Win32类(例如DesktopMonitor,Keyboard或Printer)。

Getting the Right Data 获取正确的数据

To get the attributes that the specific class will return (ie Name, Manufacturer and DeviceID as seen above to name a few), look at the documentation for that class. 要获取特定类将返回的属性(例如,名称,制造商和设备ID,如上所示),请查看该类的文档。

Documentation for peripherals and other computer hardware can be found on the MSDN Computer System Hardware Classes page. 外围设备和其他计算机硬件的文档可以在“ MSDN计算机系统硬件类”页面上找到。

The attribute you are looking for to get a unique device identifier is DeviceID, which should be an attribute on pretty much all the hardware classes or peripherals you need to use. 您要获取唯一设备标识符的属性是DeviceID,它应该是几乎所有您需要使用的硬件类或外围设备上的属性。

Manufacturers are not available on all classes, such as Win32_Keyboard , but should be available on many by referencing the Manufacturer property. 制造商并非在所有类上都可用,例如Win32_Keyboard ,但是通过引用Manufacturer属性可以在许多类上使用Manufacturer

The model of the device is not normally documented consistently by Windows in WMI classes, but if it is available it will normally be located under the Name property. Windows在WMI类中通常不会一致地记录设备的模型,但是如果可用,则通常位于Name属性下。

That should cover everything you're confused about, but let me know if there's anything else I can help with! 那应该涵盖您所困惑的所有内容,但是如果有其他需要帮助的地方,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM