{"id":57,"date":"2013-06-14T15:21:45","date_gmt":"2013-06-14T15:21:45","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/mle\/?p=57"},"modified":"2016-06-06T11:10:46","modified_gmt":"2016-06-06T11:10:46","slug":"determining-the-number-of-monitors","status":"publish","type":"post","link":"https:\/\/blog.soton.ac.uk\/mle\/2013\/06\/14\/determining-the-number-of-monitors\/","title":{"rendered":"Determining the number of monitors"},"content":{"rendered":"<p>This is a fairly technical post explaining how I managed to get C# to show the number of physical monitors connected to a PC.<\/p>\n<p>My problem was that when connected through VNC or Remote Desktop (or even when I wasn&#8217;t) our computers were still reporting that they had a resolution and a default monitor.<\/p>\n<p><!--more--><\/p>\n<p>After trying fairly standard code such as<\/p>\n<p><code>Screen [] screens = Screen.AllScreens;<\/code><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/1538602\/find-number-and-resolution-to-all-monitors\" target=\"_blank\">Source<\/a><\/p>\n<p>Or even WMI queries such as<\/p>\n<p><code>ManagementObjectSearcher searcher = new ManagementObjectSearcher(\"root\\\\CIMV2\", \"SELECT * FROM Win32_DesktopMonitor\");\u00a0 <\/code><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/3477077\/get-pcs-monitor-information-using-net-wmi\" target=\"_blank\">Source<\/a><\/p>\n<p>All these queries returned that a monitor, normally Default Display, was connected to the computer. As students occasionally unplug a monitor to dual display a second PC I needed be able to programmatically\u00a0confirm that the screen is actually connected.<\/p>\n<p>In the end, and I&#8217;m writing this down so that anyone else in my situation can find it through Google, the following code worked (thanks to <a href=\"http:\/\/stackoverflow.com\/a\/7468393\" target=\"_blank\">rix0rrr<\/a>)<\/p>\n<pre>try\r\n{\r\n    ManagementObjectSearcher searcher =\r\n        new ManagementObjectSearcher(\"root\\\\CIMV2\",\r\n        \"SELECT * FROM Win32_PnPEntity where service=\\\"monitor\\\"\");\r\n\r\n    int numberOfMonitors = searcher.Get().Count;\r\n\r\n    Console.WriteLine(\"Number of Monitors = \" + numberOfMonitors);\r\n\r\n}\r\ncatch (ManagementException e)\r\n{\r\n    MessageBox.Show(\"An error occurred while querying for WMI data: \" + e.Message);\r\n}<\/pre>\n<p>You&#8217;ll need to remember to have\u00a0<code>using System.Management;<\/code> and add a new reference to System.Management before\u00a0ManagementObjectSearcher will work correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a fairly technical post explaining how I managed to get C# to show the number of physical monitors connected to a PC. My problem was that when connected through VNC or Remote Desktop (or even when I wasn&#8217;t) our computers were still reporting that they had a resolution and a default monitor.<\/p>\n","protected":false},"author":63858,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29468],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-technical","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/users\/63858"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":17,"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":1482,"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/posts\/57\/revisions\/1482"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/mle\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}