This is a fairly technical post explaining how I managed to get C# to show the number of physical monitors connected to a PC.

My problem was that when connected through VNC or Remote Desktop (or even when I wasn’t) our computers were still reporting that they had a resolution and a default monitor.

After trying fairly standard code such as

Screen [] screens = Screen.AllScreens;
Source

Or even WMI queries such as

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DesktopMonitor"); 
Source

All these queries returned that a monitor, normally Default Display, was connected to the computer. As students occasionally unplug a monitor to dual display a second PC I needed be able to programmatically confirm that the screen is actually connected.

In the end, and I’m writing this down so that anyone else in my situation can find it through Google, the following code worked (thanks to rix0rrr)

try
{
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher("root\\CIMV2",
        "SELECT * FROM Win32_PnPEntity where service=\"monitor\"");

    int numberOfMonitors = searcher.Get().Count;

    Console.WriteLine("Number of Monitors = " + numberOfMonitors);

}
catch (ManagementException e)
{
    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}

You’ll need to remember to have┬áusing System.Management; and add a new reference to System.Management before┬áManagementObjectSearcher will work correctly.