Detect if monitor is on c#

前端 未结 3 866
谎友^
谎友^ 2020-12-31 05:58

Is it possible to detect if the users monitor is turned on using c#?

Sp

3条回答
  •  抹茶落季
    2020-12-31 06:27

    WMI might help.

    In Vista+, there is the WmiMonitorBasicDisplayParams class, where the "Active" property tells you if the display is active.

    Here's an example which works for me:

    using System.Management;
    
    // ...
    
    var query = "select * from WmiMonitorBasicDisplayParams";
    using(var wmiSearcher = new ManagementObjectSearcher("\\root\\wmi", query))
    {
        var results = wmiSearcher.Get();
        foreach (ManagementObject wmiObj in results)
        {
            // get the "Active" property and cast to a boolean, which should 
            // tell us if the display is active. I've interpreted this to mean "on"
            var active = (Boolean)wmiObj["Active"];
        }
    }
    

提交回复
热议问题