检测已连接显示器

之前在写C#操作电脑多显示器设置时,就一直在想如何判断当前有几个显示器。虽然使用System.Windows.Forms.Screen.AllScreens是可以获取到多个屏幕的,但是在多显示器设置为复制屏后,System.Windows.Forms.Screen.AllScreens就不好使了,返回的就只有一个了,最近发现一个方式能够在复制屏下也能获取到多个显示器,示例如下:

先引用System.Management.dll

 /// <summary>
/// 获取屏幕数量
/// </summary>
/// <returns></returns>
public int GetMonitorCount()
{
    string text = string.Empty;
    int count = 0;
    ManagementObjectSearcher mos = new ManagementObjectSearcher(@"rootwmi", "Select * from WmiMonitorID");
    foreach (ManagementObject mo in mos.Get())
    {
        text += mo.GetText(TextFormat.Mof);
        count++;
    }
    return count;
}

当我把所有显示器都移除后,该方法仍然返回1,所以还是没有办法判断到当前主机是否有连接显示器,如果您有判断方法,请告知我。

原文地址:https://www.cnblogs.com/zzr-stdio/p/12837130.html