I am trying to get the system device IDs from the device manager, in C#. I found some code to find the USB device ID, but I don't know how to change the code from USB device to PCI device.
This is the code that I found:
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_SystemDevices WHERE InterfaceType='USB'");
foreach (ManagementObject mo in mos.Get())
{
ManagementObject query = new ManagementObject("Win32_PhysicalMedia.Tag='" + mo["DeviceID"] + "'");
Console.WriteLine(query["SerialNumber"]);
}
According to MSDN, Win32_PhysicalMedia represents any type of documentation or storage medium.
If you want to get DeviceID from PCI device (like as in Device Manager at Control Panel) - you need Win32_PnPEntity class, which represents the properties of a Plug and Play device. So, try to use this code:
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
Console.WriteLine("Description: {0}", queryObj["Description"]);
}
Running this code provides me lots of info about my PCI devices
Related
I'm using the following code to get my drive serial number. It's working fine with Windows 7, 8, 8.1, and 10 Professional, but I'm getting an error on Windows 10 Home.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{
if (wmi_HD["SerialNumber"] == null)
hddId = null;
else
hddId = wmi_HD["SerialNumber"].ToString();
}
I'm getting
System.NullReferenceException : Object reference not set to an instance of an object.
Does anyone know why? What do I need to do to get the serial number in this case?
One more question: if I boot the OS from my pendrive, will this code work? How could I know that the OS is running from a pendrive or disk or any other resource?
When I go to the Device Manager, I see this:
I am adding this as an answer because it can save lot of time while debugging scenarios like System.NullReferenceException in WMI.
Windows+R (run command)
Type wbemtest
And connect to the machine for which you want to fetch information. Fire the query for Win32_DiskDrive and check the output for properties that you can fetch.
This is what I'm using on Windows 10 v1809:
using System;
using System.Management;
namespace GetSerialNo
{
class Program
{
static void Main(string[] args)
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject info in searcher.Get())
{
Console.WriteLine("DeviceID: " + info["DeviceID"].ToString());
Console.WriteLine("Model: " + "Model: " + info["Model"].ToString());
Console.WriteLine("Interface: " + "Interface: " + info["InterfaceType"].ToString());
Console.WriteLine("Serial#: " + "Serial#: " + info["SerialNumber"].ToString());
}
Console.ReadLine();
}
}
}
For details please see http://csharphelper.com/blog/2017/10/get-hard-drive-serial-number-c/
For the associated link Get Hard disk serial Number given by #ADreNaLiNe-DJ I wasn't able to find the required assembly reference for HardDrive hd = new HardDrive();
How do I get the port details of the Bluetooth device that I have paired within my windows form c# application?
Manually I can get all port names but I need the com port name that allocated to particular Bluetooth Device.
Check this post
the Win32_PnPEntity is Plug and play devices MSDN
you can go on the drivers also to find your device
// The WMI query
const string QueryString = "SELECT * FROM Win32_PnPSignedDriver ";
SelectQuery WMIquery = new SelectQuery(QueryString);
ManagementObjectSearcher WMIqueryResults = new ManagementObjectSearcher(WMIquery);
// Make sure results were found
if (WMIqueryResults == null)
return;
// Scan query results to find port
ManagementObjectCollection MOC = WMIqueryResults.Get();
foreach (ManagementObject mo in MOC)
{
if (mo["FriendlyName"] != null && mo["FriendlyName"].ToString().Contains("YOUR_DEVICE_NAME"))
{}
//Check the mo Properties to find the COM port
}
I want to fetch a unique ID specific to my headphones connected as a PnP to my system.
This ID should be same when I connect headphones to some other system.
I have tried using DeviceId and PNPDeviceId, but I am getting different values when connected to other systems.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["Description"].ToString().ToLower().Contains("audio"))
{
Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
Console.WriteLine("Description: {0}", queryObj["Description"]);
Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);
}
}
Below are the values I got for the same headphones:
DeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_17AA3070&REV_1001\4&A87FB78&0&0001
Description: High Definition Audio Device
PNPDeviceID: HDAUDIO\FUNC_01&VEN_10EC&DEV_0662&SUBSYS_17AA3070&REV_1001\4&A87FB78&0&0001
and
DeviceID: HDAUDIO\FUNC_01&VEN_11D4&DEV_1984&SUBSYS_10280211&REV_1004\4&851744B&0&0001
Description: High Definition Audio Device
PNPDeviceID: HDAUDIO\FUNC_01&VEN_11D4&DEV_1984&SUBSYS_10280211&REV_1004\4&851744B&0&0001
Is there any way to get a unique ID for the device?
I am using WMI query to detect USB to serial port but the problem is that in windows 7 the application takes long time to start while in windows xp it is working fine. I am using wmi query in following way
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPDevice");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["SameElement"].ToString().Contains("SerialPort"))
{
//do something
}
}
According to my reasoning it is happening due to large number of Pnp Devices and querying for serial port from that list. I tried using Win32_SerialPort but it working in windows xp while on my laptop(windows 7) it shows message not supported even though their are virtual serial port and USB to serial ports. It doesn't work even from administrator account. MSSerial_PortName also doesn't work on my laptop(windows7). Thus is their any way to make my application start faster in windows 7 using WMI query?
Win32_PnPDevice takes a long time
MSSerial_PortName works on my Win 7 laptop.
Try this (should work, modified from a program I had):
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
serialPort = new SerialPort(queryObj["PortName"].ToString(), 115200, Parity.None, 8, StopBits.One);//change parameters
//If the serial port's instance name contains USB
//it must be a USB to serial device
if (queryObj["InstanceName"].ToString().Contains("USB"))//if you have a VID or PID name then this should not be nessesery
{
//should get serial to usb adapters
try
{
serialPort.Open();
if (queryObj["InstanceName"].ToString().Contains(VID_or_PID))
{
//do sth
}
serialPort.Close();
}
catch (Exception ex)
{
//exception handling
}
}
}
}
catch (ManagementException ex)
{
//write("Querying for WMI data. Exception raised(" + ex.Message + "): " + ex);
}
Is there anyway using WMI/.Net to grab monitor information such as Manufacturer, Serial Number, Monitor Size etc.?
Using a script is an option as well, or can I query the registry directly to get this information?
SELECT * FROM Win32_DesktopMonitor doesn't really return any useful information for me in this case.
Hey, I use this tool for a lot of my WMI work, especially when prototyping and creating POCs....
Microsoft WMI Code Generator
This tool is great for creating quick console app code for any wmi query or method invocation in both C# and VB.NET
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_DesktopMonitor");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_DesktopMonitor instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("Description: {0}", queryObj["Description"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
The code above will get you the make and model of the monitor.
You may want to try this
https://raw.githubusercontent.com/MaxAnderson95/Get-Monitor-Information/master/Get-Monitor.ps1
Cheers
That select query should give you what you want. Here is the documentation which contains the details of the query.
Then you could do something like this:
public void GetMonitorDetails()
{
using(ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor")
{
foreach(ManagementObject currentObj in searcher.Get())
{
String name = currentObj("Name").ToString();
String device_id = currentObj("DeviceID").ToString();
// ...
}
}
}
This post, combined with the answer below about the WMI management tool had my answer. Here is the code that returns your monitor resolutions.
try {
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM WmiMonitorBasicDisplayParams");
foreach (ManagementObject queryObj in searcher.Get()) {
Debug.WriteLine("-----------------------------------");
Debug.WriteLine("WmiMonitorBasicDisplayParams instance");
Debug.WriteLine("-----------------------------------");
Debug.WriteLine("Description: {0}", queryObj["SupportedDisplayFeatures"]);
}
} catch (ManagementException e) {
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
In my case, I'm still stuck, because it is returning the "scaled down" resolution of each monitor. One of mine is a 4K display, being reported as 2560x1440.