Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of monitors connected to the PC in C#?

Tags:

c#

screen

wpf

I need to detect the number of monitors that are physically connected to the computer (to decide if the screen configuration is in single, extended, or duplicate mode).

Both System.Windows.Forms.Screen.AllScreens.Length and System.Windows.Forms.SystemInformation.MonitorCount return the number of virtual screens (desktops).

I need that if there are 2+ monitors connected to the PC, I can decide between duplicate/extended mode using this value, but I can't decide if there is only one physical monitor connected to the PC, and therefore the screen configuration is in single screen mode.

like image 762
Dominik Palo Avatar asked Dec 04 '25 09:12

Dominik Palo


2 Answers

Try the following:

    System.Management.ManagementObjectSearcher monitorObjectSearch = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
    int Counter = monitorObjectSearch.Get().Count;

Answer found on the following question:

WMI Get All Monitors Not Returning All Monitors

Update

Try the following function it detects unplugging the monitor:

    private int GetActiveMonitors()
    {
        int Counter = 0;
        System.Management.ManagementObjectSearcher monitorObjectSearch = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
        foreach (ManagementObject Monitor in monitorObjectSearch.Get())
        {
            UInt16 Status = 0;
            try
            {
                Status = (UInt16)Monitor["Availability"];
            }
            catch (Exception ex)
            {
                //Error handling if you want to
                continue;
            }
            if (Status == 3)
                Counter++;

        }
        return Counter;
    }

Here are a list for the status: https://msdn.microsoft.com/en-us/library/aa394122%28v=vs.85%29.aspx

Maybe you need to increase the counter on a other statuscode as well. Check the link for more information.

like image 191
Simon Aberle Avatar answered Dec 05 '25 23:12

Simon Aberle


Based on the Xanatos answer, I created simple helper class to detect the screens configuration:

using System;
using System.Management;
using System.Windows.Forms;

public static class ScreensConfigurationDetector
{
    public static ScreensConfiguration GetConfiguration()
    {
        int physicalMonitors = GetActiveMonitors();
        int virtualMonitors = Screen.AllScreens.Length;

        if (physicalMonitors == 1)
        {
            return ScreensConfiguration.Single;
        }

        return physicalMonitors == virtualMonitors 
            ? ScreensConfiguration.Extended 
            : ScreensConfiguration.DuplicateOrShowOnlyOne;
    }

    private static int GetActiveMonitors()
    {
        int counter = 0;
        ManagementObjectSearcher monitorObjectSearch = new ManagementObjectSearcher("SELECT * FROM Win32_DesktopMonitor");
        foreach (ManagementObject Monitor in monitorObjectSearch.Get())
        {
            try
            {
                if ((UInt16)Monitor["Availability"] == 3)
                {
                    counter++;
                }
            }
            catch (Exception)
            {
                continue;
            }

        }
        return counter;
    }
}

public enum ScreensConfiguration
{
    Single,
    Extended,
    DuplicateOrShowOnlyOne
}
like image 30
Dominik Palo Avatar answered Dec 05 '25 21:12

Dominik Palo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!