Get PC's Monitor Information Using .NET / WMI - c#

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.

Related

Detect what VPN I'm connected to

I'm currently working on an desktop tool to automate VPN connections to customers. I'd like to have a good way to know for sure I'm indeed connected to a VPN. I'm currently thinking about using the routing table as it's shown in netstat-rn and comparing it to the IP I should be connected to.
My question is, how would I be able to get these IP's in my C# application, or is there a better way to determine if I'm connected to a VPN.
Thanks in advance!
This is so classic, I spend a couple of hours looking for the solution, and when I ask for help I find it myself.
For those who wonder, this as the solution.
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_IP4RouteTable");
ListViewItem buf;
foreach (ManagementObject queryObj in searcher.Get())
{
string destination = queryObj["Destination"].ToString();
string mask = queryObj["Mask"].ToString();
string metric = queryObj["Metric1"].ToString();
string interfaceIndex = queryObj["InterfaceIndex"].ToString();
string nexthop = queryObj["NextHop"].ToString();
string protocol =queryObj["Protocol"].ToString();
string type = queryObj["Type"].ToString();
string status;
if (queryObj["Status"]!=null)
{
status = queryObj["Status"].ToString();
}
else
{
status = string.Empty;
}
buf = new ListViewItem(new string[] {destination,mask,metric,interfaceIndex,nexthop,protocol,status,typ});
list_route.Items.Add(buf);
}
}
catch (ManagementException ex)
{
MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message);
}
Anyway, thanks everyone who took the time to try and help me!

Unable to get system hard drive SerialNumber using Win32_DiskDrive

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();

Why "root\\StandardCimv2" in Windows 7 doesn't work?

workWhen i try show list with adapters name in combobox like this:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\StandardCimv2","SELECT * FROM MSFT_NetAdapter");
foreach (ManagementObject queryObj in searcher.Get())
{
try
{
comboBox1.Items.Add(queryObj["Name"].ToString());
}
catch (Exception)
{
comboBox1.Items.Add("");
}
}
All good...because used Windows 8. If i run app on PC with Windows 7 i get error:
System.Management.ManagementException: Invalid namespace
Later I noticed one thing...if this line code:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\StandardCimv2","SELECT * FROM MSFT_NetAdapter")
Replace on this:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapter");
The error disappears and everything works. And now Question!
Why this "root\StandardCimv2" not work in Win7? But with this "root\CIMV2" all good.
Thanks in advance.
The Win32_NetworkAdapter class works on Vista and 7.
This answer has an example of using it (in PowerShell) to retrieve Name and connection Speed:
https://stackoverflow.com/a/3002568/550712
It looks like it also has the "Name" property so it might work as a direct replacement in your code.

Not all Groups are returned via WMI

Using the code below not all Group names are returned if I compare to computer management. Am I doing something wrong?
UPDATE: It does update if i restart my application
C# Code
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_GroupUser");
foreach (ManagementObject queryObj in searcher.Get())
{
String sGroupName = queryObj["GroupComponent"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
String sUsername = queryObj["PartComponent"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
}
}
catch (ManagementException exception)
{
MessageBox.Show("An error occurred while querying for WMI data: " + exception.Message);
}
I believe Win32_GroupUser is a relationship between users and groups.
If a group has no users, it will not be included.
Try querying for Win32_Group.
I was binding the results to a grid and you have to set the datasource to null first - this was my issue.

How to get the Device ID of PCI devices?

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

Categories

Resources