KevinM1990112qwq
KevinM1990112qwq

Reputation: 735

How to use WMI to get monitor information for all attached monitors?

I am building a simple program that when it is ran it will show different computer components.

Right now I am trying to show all monitors plugged in. The name of each and the total amount plugged into the computer.

When I run the below code, It only shows me the name of one monitor even though I am plugged into three. Could someone please tell me why.

public static List<string> GetMonitorNames()
{
    ManagementClass mc = new ManagementClass("Win32_DesktopMonitor");
    ManagementObjectCollection moc = mc.GetInstances();
    var info = new List<string>();
    foreach (ManagementObject mo in moc)
    {
        info.Add(mo["Name"].ToString());
    }
    return info;
}

Upvotes: 1

Views: 4532

Answers (2)

theB
theB

Reputation: 6738

Not a direct answer to the question as posed, but rather than deferring to WMI, why not use a solution available to you in .net, like this Winforms solution:

using System.Diagnostics;
using System.Windows.Forms;

Debug.Print("Total Number Of Monitors: {0}", Screen.AllScreens.Length);
foreach (Screen scr in Screen.AllScreens)
{
    Debug.Print(scr.DeviceName);
}

To clarify based on your comment, yes the method can be used from within a static method, as in:

public static List<string> GetAllMonitorNames()
{
    List<string> result = new List<string>();
    foreach (Screen scr in Screen.AllScreens)
    {
        result.Add(scr.DeviceName);
    }
    return result;
}

or using LINQ if you prefer:

public static IEnumerable<string> GetAllMonitorNames() => 
            Screen.AllScreens.Select((s) => s.DeviceName);

You can also still just P/Invoke the API's EnumDisplayMonitors, and add the display names to the list. (pinvoke.net example)

Upvotes: 2

hawkstrider
hawkstrider

Reputation: 4341

You can get the instance names of all monitiors using the WmiMonitorBasicDisplayParams method.

Here is an example

public static List<string> GetMonitorNames()
{
    ManagementObjectSearcher searcher =
            new ManagementObjectSearcher("root\\WMI",
            "SELECT * FROM WmiMonitorBasicDisplayParams");
    var info = new List<string>();
    foreach (ManagementObject queryObj in searcher.Get()) { 
        info.Add(queryObj["InstanceName"].ToString());
    }
    return info;
}

Example output from my machine and available properties on this object

Active                        : True
DisplayTransferCharacteristic : 120
InstanceName                  : DISPLAY\HWP3270\4&7563392&0&UID16843008_0
MaxHorizontalImageSize        : 52
MaxVerticalImageSize          : 32
SupportedDisplayFeatures      : WmiMonitorSupportedDisplayFeatures
VideoInputType                : 0
PSComputerName                :

Active                        : True
DisplayTransferCharacteristic : 120
InstanceName                  : DISPLAY\HWP326E\4&7563392&0&UID53349120_0
MaxHorizontalImageSize        : 52
MaxVerticalImageSize          : 32
SupportedDisplayFeatures      : WmiMonitorSupportedDisplayFeatures
VideoInputType                : 1
PSComputerName                :

Upvotes: 2

Related Questions