简体   繁体   中英

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. 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.

For monitor information

select * from win32_pnpentity where PNPClass = 'Monitor'

There are 2 kinds of printers local or network. For local printers, Win32_Printer will work.

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

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. Once you've added it as a reference, remember to include 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. 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 .

Using 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.

I have also implemented this exact situation in the example project on GitHub so that you know what to do.

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).

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.

Documentation for peripherals and other computer hardware can be found on the MSDN Computer System Hardware Classes page.

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.

Manufacturers are not available on all classes, such as Win32_Keyboard , but should be available on many by referencing the Manufacturer property.

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.

That should cover everything you're confused about, but let me know if there's anything else I can help with!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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