[英]Detect number of display monitor
我已经尝试过Screen.AllScreen,SystemInformation.MonitorCount和WMI,但它们都失败了。
我的应用程序作为Windows服务运行,因此没有可视的Form或UI。 即使我有2个监视器,Screen.AllScreen和SystemInformation.MonitorCount都返回1。 如果我在控制台中运行应用程序,它将返回正确的显示计数,但是我的要求是我的应用程序必须以Windows服务(无UI)运行。
谢谢!
找到了我自己的问题的答案。 仍然最终使用WMI。
我最初使用Win32_DesktopMonitor给出了不可靠的答案。
使用此查询:
"SELECT * FROM Win32_PnPEntity WHERE Service = 'monitor'"
WMI返回连接到我的PC的正确监视器实例。
我使用Win32_PnPEntity是因为它表示设备管理器中即插即用设备的信息,当您插入监视器时,它将向您显示。查询在“搜索器”中的工作方式比其他方式更准确,因为使用Like运算符。 我这样做是因为在3台不同的计算机上,设备管理器中的监视器条目出现的方式有所不同。 例如(即插即用监视器,即插即用监视器(标准),通用即插即用监视器)。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.