Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for hasmonitor.cs

(view source code of hasmonitor.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management;
  5. using System.Windows.Forms;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	internal class HasMonitor
  11. 	{
  12. 		static readonly string progver = "1.00";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			int defaultmonitors;
  18. 			int includedefmon = 0;
  19. 			int monitorcount;
  20. 			string wmiquery;
  21. 			bool listall = false;
  22. 			bool quietmode = false;
  23.  
  24. 			if ( args.Length > 0 )
  25. 			{
  26. 				foreach ( string arg in args )
  27. 				{
  28. 					switch ( arg.Split( ':' )[0].ToUpper( ) )
  29. 					{
  30. 						case "/?":
  31. 							return ShowHelp( );
  32. 						case "/A":
  33. 						case "/ALL":
  34. 							if ( listall )
  35. 							{
  36. 								return ShowHelp( "Duplicate command line switch /A" );
  37. 							}
  38. 							listall = true;
  39. 							break;
  40. 						case "/D":
  41. 							if ( includedefmon != 0 )
  42. 							{
  43. 								return ShowHelp( "Duplicate command line switch /D" );
  44. 							}
  45. 							if ( arg.Split( ':' ).Length > 1 )
  46. 							{
  47. 								if ( !int.TryParse( arg.Split( ':' )[1], out includedefmon ) )
  48. 								{
  49. 									return ShowHelp( "Invalid number in argument \"{0}\"", arg );
  50. 								}
  51. 							}
  52. 							else
  53. 							{
  54. 								includedefmon = -1;
  55. 							}
  56. 							break;
  57. 						case "/Q":
  58. 						case "/QUIET":
  59. 							if ( quietmode )
  60. 							{
  61. 								return ShowHelp( "Duplicate command line switch /Q" );
  62. 							}
  63. 							quietmode = true;
  64. 							break;
  65. 						default:
  66. 							return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  67. 					}
  68. 				}
  69. 			}
  70.  
  71. 			if ( listall )
  72. 			{
  73. 				monitorcount = Screen.AllScreens.Length;
  74. 				if ( !quietmode )
  75. 				{
  76. 					Console.Write( "AllScreens reports " );
  77. 					if ( monitorcount == 0 )
  78. 					{
  79. 						Console.ForegroundColor = ConsoleColor.Red;
  80. 					}
  81. 					else
  82. 					{
  83. 						Console.ForegroundColor = ConsoleColor.Green;
  84. 					}
  85. 					Console.Write( monitorcount );
  86. 					Console.ResetColor( );
  87. 					Console.WriteLine( " monitor{0}{1}", ( monitorcount == 1 ? string.Empty : "s" ), ( monitorcount == 0 ? string.Empty : ":" ) );
  88. 					foreach ( Screen screen in Screen.AllScreens )
  89. 					{
  90. 						Console.WriteLine( "\t{0} ({1}x{2})", screen.DeviceName, screen.Bounds.Width, screen.Bounds.Height );
  91. 					}
  92. 					Console.WriteLine( );
  93. 				}
  94.  
  95. 				wmiquery = "SELECT * FROM Win32_DesktopMonitor";
  96. 			}
  97. 			else if ( includedefmon != 0 )
  98. 			{
  99. 				wmiquery = "SELECT * FROM Win32_DesktopMonitor";
  100. 			}
  101. 			else
  102. 			{
  103. 				// Using this WMI query to check for disconnected monitors will only work
  104. 				// if specific drivers have been installed for all monitors, monitors
  105. 				// installed as "Default Monitor" will be considered disconnected
  106. 				wmiquery = "SELECT * FROM Win32_DesktopMonitor WHERE MonitorManufacturer IS NOT NULL";
  107. 			}
  108.  
  109. 			ManagementObjectSearcher searcher = new ManagementObjectSearcher( wmiquery );
  110. 			ManagementObjectCollection colItems = searcher.Get( );
  111. 			monitorcount = colItems.Count;
  112.  
  113. 			if ( includedefmon > 0 )
  114. 			{
  115. 				defaultmonitors = 0;
  116. 				foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
  117. 				{
  118. 					if ( queryObj["MonitorType"].ToString( ) == "Default Monitor" )
  119. 					{
  120. 						defaultmonitors += 1;
  121. 					}
  122. 				}
  123. 				if ( defaultmonitors > 0 )
  124. 				{
  125. 					monitorcount = Math.Max( ( monitorcount - includedefmon ), ( monitorcount - defaultmonitors ) );
  126. 					monitorcount = Math.Max( monitorcount, 0 );
  127. 				}
  128. 			}
  129.  
  130. 			if ( !quietmode )
  131. 			{
  132. 				if ( listall )
  133. 				{
  134. 					defaultmonitors = 0;
  135. 					Console.Write( "WMI reports " );
  136. 					if ( monitorcount == 0 )
  137. 					{
  138. 						Console.ForegroundColor = ConsoleColor.Red;
  139. 					}
  140. 					else
  141. 					{
  142. 						Console.ForegroundColor = ConsoleColor.Green;
  143. 					}
  144. 					Console.Write( monitorcount );
  145. 					Console.ResetColor( );
  146. 					Console.WriteLine( " monitor{0}{1}", ( monitorcount == 1 ? string.Empty : "s" ), ( monitorcount == 0 ? string.Empty : ":" ) );
  147. 					foreach ( ManagementObject queryObj in colItems.Cast<ManagementObject>( ) )
  148. 					{
  149. 						string monitordescription = string.Format( "{0} {1}", queryObj["MonitorManufacturer"], queryObj["MonitorType"] );
  150. 						if ( queryObj["MonitorType"].ToString( ) == "Default Monitor" )
  151. 						{
  152. 							defaultmonitors += 1;
  153. 						}
  154. 						try
  155. 						{
  156. 							if ( !string.IsNullOrEmpty( queryObj["ScreenWidth"].ToString( ) ) )
  157. 							{
  158. 								monitordescription = string.Format( "{0} ({1}x{2})", monitordescription, queryObj["ScreenWidth"], queryObj["ScreenHeight"] );
  159. 							}
  160. 						}
  161. 						catch ( NullReferenceException )
  162. 						{
  163. 							// ignore
  164. 						}
  165. 						monitordescription = "\t" + monitordescription.Trim( );
  166. 						Console.WriteLine( monitordescription );
  167. 					}
  168. 					Console.WriteLine( );
  169. 					if ( defaultmonitors > 0 )
  170. 					{
  171. 						Console.WriteLine( "WMI reported {0} \"Default Monitor{1}\".", defaultmonitors, ( defaultmonitors > 1 ? "s" : string.Empty ) );
  172. 						Console.WriteLine( "If {0} real monitor{1}, connected to the computer, use this", ( defaultmonitors > 1 ? "these are" : "this is a" ), ( defaultmonitors > 1 ? "s" : string.Empty ) );
  173. 						Console.Write( "program's " );
  174. 						Console.ForegroundColor = ConsoleColor.Yellow;
  175. 						Console.Write( "/D" );
  176. 						Console.ResetColor( );
  177. 						Console.WriteLine( " switch to include {0} Default Monitor{1}, otherwise", ( defaultmonitors == 1 ? "this" : "these" ), ( defaultmonitors > 1 ? "s" : string.Empty ) );
  178. 						Console.WriteLine( "{0} will be considered disconnected.", ( defaultmonitors == 1 ? "it" : "they" ) );
  179. 						ShowHelp( );
  180. 					}
  181. 				}
  182. 				else
  183. 				{
  184. 					Console.Write( "Monitor{0} found: ", ( monitorcount == 1 ? string.Empty : "s" ) );
  185. 					if ( monitorcount == 0 )
  186. 					{
  187. 						Console.ForegroundColor = ConsoleColor.Red;
  188. 					}
  189. 					else
  190. 					{
  191. 						Console.ForegroundColor = ConsoleColor.Green;
  192. 					}
  193. 					Console.WriteLine( monitorcount );
  194. 					Console.ResetColor( );
  195. 				}
  196. 			}
  197.  
  198. 			return monitorcount;
  199. 		}
  200.  
  201.  
  202. 		public static int ShowHelp( params string[] errmsg )
  203. 		{
  204. 			#region Error Message
  205.  
  206. 			if ( errmsg.Length > 0 )
  207. 			{
  208. 				List<string> errargs = new List<string>( errmsg );
  209. 				errargs.RemoveAt( 0 );
  210. 				Console.Error.WriteLine( );
  211. 				Console.ForegroundColor = ConsoleColor.Red;
  212. 				Console.Error.Write( "ERROR:\t" );
  213. 				Console.ForegroundColor = ConsoleColor.White;
  214. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  215. 				Console.ResetColor( );
  216. 			}
  217.  
  218. 			#endregion Error Message
  219.  
  220.  
  221. 			#region Help Text
  222.  
  223. 			/*
  224. 			HasMonitor.exe,  Version 1.00
  225. 			Check the actual number of monitors connected at this very moment
  226.  
  227. 			Usage:   HasMonitor.exe  [ /A | /D[:number] ]  [ /Q ]
  228.  
  229. 			Where:   /A            list All monitors, including temporarily disconnected
  230. 			                       ones (default: list only connected monitors)
  231. 			         /D[:number]   include (number of) Default Monitor(s)
  232. 			                       (default: assume any Default Monitor is diconnected)
  233. 			         /Q            Quiet mode (no screen output)
  234.  
  235. 			Notes:   Use this program's /A switch when run for the first time. If it
  236. 			         reports one or more Default Monitors, check if these are real
  237. 			         monitors, connected to the computer, or disconnected monitors.
  238. 			         By default, this program treats Default Monitors as disconnected
  239. 			         ones, but you can include a number of Default Monitors with the
  240. 			         /D switch. Assuming 2 Default Monitors were reported, and 1 is a
  241. 			         real monitor, use /D:1, if both are real use /D:2 or /D.
  242. 			         The program's return code equals the number of monitors connected,
  243. 			         or with /A switch the total number including disconnected ones,
  244. 			         or -1 in case of (command line) errors.
  245.  
  246. 			Written by Rob van der Woude
  247. 			https://www.robvanderwoude.com
  248. 			*/
  249.  
  250. 			#endregion Help Text
  251.  
  252.  
  253. 			#region Display Help Text
  254.  
  255. 			Console.Error.WriteLine( );
  256.  
  257. 			Console.Error.WriteLine( "HasMonitor.exe,  Version {0}", progver );
  258.  
  259. 			Console.Error.WriteLine( "Check the actual number of monitors connected at this very moment" );
  260.  
  261. 			Console.Error.WriteLine( );
  262.  
  263. 			Console.Error.Write( "Usage:   " );
  264. 			Console.ForegroundColor = ConsoleColor.White;
  265. 			Console.Error.WriteLine( "HasMonitor.exe  [ /A | /D[:number] ]  [ /Q ]" );
  266. 			Console.ResetColor( );
  267.  
  268. 			Console.Error.WriteLine( );
  269.  
  270. 			Console.Error.Write( "Where:   " );
  271. 			Console.ForegroundColor = ConsoleColor.White;
  272. 			Console.Error.Write( "/A" );
  273. 			Console.ResetColor( );
  274. 			Console.Error.Write( "            list " );
  275. 			Console.ForegroundColor = ConsoleColor.White;
  276. 			Console.Error.Write( "A" );
  277. 			Console.ResetColor( );
  278. 			Console.Error.WriteLine( "ll monitors, including temporarily disconnected" );
  279. 			Console.Error.WriteLine( "                       ones (default: list only connected monitors)" );
  280.  
  281. 			Console.ForegroundColor = ConsoleColor.White;
  282. 			Console.Error.Write( "         /D[:number]" );
  283. 			Console.ResetColor( );
  284. 			Console.Error.Write( "   include (" );
  285. 			Console.ForegroundColor = ConsoleColor.White;
  286. 			Console.Error.Write( "number" );
  287. 			Console.ResetColor( );
  288. 			Console.Error.WriteLine( " of) Default Monitor(s)" );
  289.  
  290. 			Console.Error.Write( "         " );
  291. 			Console.ForegroundColor = ConsoleColor.White;
  292. 			Console.Error.Write( "/Q            Q" );
  293. 			Console.ResetColor( );
  294. 			Console.Error.WriteLine( "uiet mode (no screen output)" );
  295.  
  296. 			Console.Error.WriteLine( );
  297.  
  298. 			Console.Error.Write( "Notes:   Use this program's " );
  299. 			Console.ForegroundColor = ConsoleColor.White;
  300. 			Console.Error.Write( "/A" );
  301. 			Console.ResetColor( );
  302. 			Console.Error.WriteLine( " switch when run for the first time. If it" );
  303.  
  304. 			Console.Error.WriteLine( "         reports one or more Default Monitors, check if these are real" );
  305.  
  306. 			Console.Error.WriteLine( "         monitors, connected to the computer, or disconnected monitors." );
  307.  
  308. 			Console.Error.WriteLine( "         By default, this program treats Default Monitors as disconnected" );
  309.  
  310. 			Console.Error.WriteLine( "         ones, but you can include a number of Default Monitors with the" );
  311.  
  312. 			Console.ForegroundColor = ConsoleColor.White;
  313. 			Console.Error.Write( "         /D" );
  314. 			Console.ResetColor( );
  315. 			Console.Error.WriteLine( " switch. Assuming 2 Default Monitors were reported, and 1 is a" );
  316.  
  317. 			Console.Error.Write( "         real monitor, use " );
  318. 			Console.ForegroundColor = ConsoleColor.White;
  319. 			Console.Error.Write( "/D:1" );
  320. 			Console.ResetColor( );
  321. 			Console.Error.Write( ", if both are real use " );
  322. 			Console.ForegroundColor = ConsoleColor.White;
  323. 			Console.Error.Write( "/D:2" );
  324. 			Console.ResetColor( );
  325. 			Console.Error.Write( " or " );
  326. 			Console.ForegroundColor = ConsoleColor.White;
  327. 			Console.Error.Write( "/D" );
  328. 			Console.ResetColor( );
  329. 			Console.Error.WriteLine( "." );
  330.  
  331. 			Console.Error.WriteLine( "         The program's return code equals the number of monitors connected," );
  332.  
  333. 			Console.Error.Write( "         or with " );
  334. 			Console.ForegroundColor = ConsoleColor.White;
  335. 			Console.Error.Write( "/A" );
  336. 			Console.ResetColor( );
  337. 			Console.Error.WriteLine( " switch the total number including disconnected ones," );
  338.  
  339. 			Console.Error.WriteLine( "         or -1 in case of (command line) errors." );
  340.  
  341. 			Console.Error.WriteLine( );
  342.  
  343. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  344.  
  345. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  346.  
  347. 			#endregion Display Help Text
  348.  
  349.  
  350. 			return -1;
  351. 		}
  352. 	}
  353. }

page last modified: 2024-04-16; loaded in 0.0225 seconds