Determine the number of monitors

I already tried Screen.AllScreen, SystemInformation.MonitorCount and WMI, but they all failed.

My application runs as a Windows service, therefore there is no visual form or user interface. Both Screen.AllScreen and SystemInformation.MonitorCount return 1, even when I have 2 monitors. If I run my application in the console, it returns the correct display count, but my requirement is that my application starts as a Windows service (without an interface).

Thank!

+2
source share
2 answers

Found an answer to my own question. Still eventually used by WMI.

I originally used Win32_DesktopMonitor, giving an unreliable answer.

:

"SELECT * FROM Win32_PnPEntity WHERE Service = 'monitor'"

WMI , .

+5

Win32_PnPEntity, Plug and Play , , . , "", , , Like. , 3 -. . (Pnp-Monitor, Pnp (), Pnp).

private int CountMonitorsInstalled()
{
    try
    {
     ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "select * from Win32_PnPEntity WHERE Name LIKE '%PnP%Monitor%'");
     return searcher.Get().Count;
    }
    catch(Exception ex)
    {
      return 0;
    }

}
-2

Source: https://habr.com/ru/post/1530671/


All Articles