using System; using System.Collections.Generic; using System.Linq; using System.Management; using System.Windows.Forms; namespace RobvanderWoude { internal class HasMonitor { static readonly string progver = "1.00"; static int Main( string[] args ) { int defaultmonitors; int includedefmon = 0; int monitorcount; string wmiquery; bool listall = false; bool quietmode = false; if ( args.Length > 0 ) { foreach ( string arg in args ) { switch ( arg.Split( ':' )[0].ToUpper( ) ) { case "/?": return ShowHelp( ); case "/A": case "/ALL": if ( listall ) { return ShowHelp( "Duplicate command line switch /A" ); } listall = true; break; case "/D": if ( includedefmon != 0 ) { return ShowHelp( "Duplicate command line switch /D" ); } if ( arg.Split( ':' ).Length > 1 ) { if ( !int.TryParse( arg.Split( ':' )[1], out includedefmon ) ) { return ShowHelp( "Invalid number in argument \"{0}\"", arg ); } } else { includedefmon = -1; } break; case "/Q": case "/QUIET": if ( quietmode ) { return ShowHelp( "Duplicate command line switch /Q" ); } quietmode = true; break; default: return ShowHelp( "Invalid command line argument \"{0}\"", arg ); } } } if ( listall ) { monitorcount = Screen.AllScreens.Length; if ( !quietmode ) { Console.Write( "AllScreens reports " ); if ( monitorcount == 0 ) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Green; } Console.Write( monitorcount ); Console.ResetColor( ); Console.WriteLine( " monitor{0}{1}", ( monitorcount == 1 ? string.Empty : "s" ), ( monitorcount == 0 ? string.Empty : ":" ) ); foreach ( Screen screen in Screen.AllScreens ) { Console.WriteLine( "\t{0} ({1}x{2})", screen.DeviceName, screen.Bounds.Width, screen.Bounds.Height ); } Console.WriteLine( ); } wmiquery = "SELECT * FROM Win32_DesktopMonitor"; } else if ( includedefmon != 0 ) { wmiquery = "SELECT * FROM Win32_DesktopMonitor"; } else { // Using this WMI query to check for disconnected monitors will only work // if specific drivers have been installed for all monitors, monitors // installed as "Default Monitor" will be considered disconnected wmiquery = "SELECT * FROM Win32_DesktopMonitor WHERE MonitorManufacturer IS NOT NULL"; } ManagementObjectSearcher searcher = new ManagementObjectSearcher( wmiquery ); ManagementObjectCollection colItems = searcher.Get( ); monitorcount = colItems.Count; if ( includedefmon > 0 ) { defaultmonitors = 0; foreach ( ManagementObject queryObj in colItems.Cast( ) ) { if ( queryObj["MonitorType"].ToString( ) == "Default Monitor" ) { defaultmonitors += 1; } } if ( defaultmonitors > 0 ) { monitorcount = Math.Max( ( monitorcount - includedefmon ), ( monitorcount - defaultmonitors ) ); monitorcount = Math.Max( monitorcount, 0 ); } } if ( !quietmode ) { if ( listall ) { defaultmonitors = 0; Console.Write( "WMI reports " ); if ( monitorcount == 0 ) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Green; } Console.Write( monitorcount ); Console.ResetColor( ); Console.WriteLine( " monitor{0}{1}", ( monitorcount == 1 ? string.Empty : "s" ), ( monitorcount == 0 ? string.Empty : ":" ) ); foreach ( ManagementObject queryObj in colItems.Cast( ) ) { string monitordescription = string.Format( "{0} {1}", queryObj["MonitorManufacturer"], queryObj["MonitorType"] ); if ( queryObj["MonitorType"].ToString( ) == "Default Monitor" ) { defaultmonitors += 1; } try { if ( !string.IsNullOrEmpty( queryObj["ScreenWidth"].ToString( ) ) ) { monitordescription = string.Format( "{0} ({1}x{2})", monitordescription, queryObj["ScreenWidth"], queryObj["ScreenHeight"] ); } } catch ( NullReferenceException ) { // ignore } monitordescription = "\t" + monitordescription.Trim( ); Console.WriteLine( monitordescription ); } Console.WriteLine( ); if ( defaultmonitors > 0 ) { Console.WriteLine( "WMI reported {0} \"Default Monitor{1}\".", defaultmonitors, ( defaultmonitors > 1 ? "s" : string.Empty ) ); 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 ) ); Console.Write( "program's " ); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write( "/D" ); Console.ResetColor( ); Console.WriteLine( " switch to include {0} Default Monitor{1}, otherwise", ( defaultmonitors == 1 ? "this" : "these" ), ( defaultmonitors > 1 ? "s" : string.Empty ) ); Console.WriteLine( "{0} will be considered disconnected.", ( defaultmonitors == 1 ? "it" : "they" ) ); ShowHelp( ); } } else { Console.Write( "Monitor{0} found: ", ( monitorcount == 1 ? string.Empty : "s" ) ); if ( monitorcount == 0 ) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Green; } Console.WriteLine( monitorcount ); Console.ResetColor( ); } } return monitorcount; } public static int ShowHelp( params string[] errmsg ) { #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Help Text /* HasMonitor.exe, Version 1.00 Check the actual number of monitors connected at this very moment Usage: HasMonitor.exe [ /A | /D[:number] ] [ /Q ] Where: /A list All monitors, including temporarily disconnected ones (default: list only connected monitors) /D[:number] include (number of) Default Monitor(s) (default: assume any Default Monitor is diconnected) /Q Quiet mode (no screen output) Notes: Use this program's /A switch when run for the first time. If it reports one or more Default Monitors, check if these are real monitors, connected to the computer, or disconnected monitors. By default, this program treats Default Monitors as disconnected ones, but you can include a number of Default Monitors with the /D switch. Assuming 2 Default Monitors were reported, and 1 is a real monitor, use /D:1, if both are real use /D:2 or /D. The program's return code equals the number of monitors connected, or with /A switch the total number including disconnected ones, or -1 in case of (command line) errors. Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Display Help Text Console.Error.WriteLine( ); Console.Error.WriteLine( "HasMonitor.exe, Version {0}", progver ); Console.Error.WriteLine( "Check the actual number of monitors connected at this very moment" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "HasMonitor.exe [ /A | /D[:number] ] [ /Q ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/A" ); Console.ResetColor( ); Console.Error.Write( " list " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "A" ); Console.ResetColor( ); Console.Error.WriteLine( "ll monitors, including temporarily disconnected" ); Console.Error.WriteLine( " ones (default: list only connected monitors)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /D[:number]" ); Console.ResetColor( ); Console.Error.Write( " include (" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "number" ); Console.ResetColor( ); Console.Error.WriteLine( " of) Default Monitor(s)" ); Console.Error.Write( " " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/Q Q" ); Console.ResetColor( ); Console.Error.WriteLine( "uiet mode (no screen output)" ); Console.Error.WriteLine( ); Console.Error.Write( "Notes: Use this program's " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/A" ); Console.ResetColor( ); Console.Error.WriteLine( " switch when run for the first time. If it" ); Console.Error.WriteLine( " reports one or more Default Monitors, check if these are real" ); Console.Error.WriteLine( " monitors, connected to the computer, or disconnected monitors." ); Console.Error.WriteLine( " By default, this program treats Default Monitors as disconnected" ); Console.Error.WriteLine( " ones, but you can include a number of Default Monitors with the" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /D" ); Console.ResetColor( ); Console.Error.WriteLine( " switch. Assuming 2 Default Monitors were reported, and 1 is a" ); Console.Error.Write( " real monitor, use " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/D:1" ); Console.ResetColor( ); Console.Error.Write( ", if both are real use " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/D:2" ); Console.ResetColor( ); Console.Error.Write( " or " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/D" ); Console.ResetColor( ); Console.Error.WriteLine( "." ); Console.Error.WriteLine( " The program's return code equals the number of monitors connected," ); Console.Error.Write( " or with " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/A" ); Console.ResetColor( ); Console.Error.WriteLine( " switch the total number including disconnected ones," ); Console.Error.WriteLine( " or -1 in case of (command line) errors." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help Text return -1; } } }