using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Management; using System.Windows.Forms; namespace RobvanderWoude { internal class CheckPath { static string progver = "1.00"; static int Main( string[] args ) { #region Parse Command Line // Only 3 valid arguments are checked, invalid arguments are ignored if ( args.Contains( "/?", StringComparer.InvariantCultureIgnoreCase ) ) { return ShowHelp( ); } bool quiet = args.Contains( "/quiet", StringComparer.InvariantCultureIgnoreCase ) || args.Contains( "/q", StringComparer.InvariantCultureIgnoreCase ); bool verbose = args.Contains( "/verbose", StringComparer.InvariantCultureIgnoreCase ) || args.Contains( "/v", StringComparer.InvariantCultureIgnoreCase ); #endregion Parse Command Line #region Initialize Variables ConsoleColor fgcolor; string[] PATH; int errors = 0; int notexist = 0; int empty = 0; #endregion Initialize Variables #region System PATH Console.WriteLine( ); Console.WriteLine( "System PATH" ); Console.WriteLine( "===========" ); string query = @"SELECT * FROM Win32_Environment WHERE Name='PATH' AND SystemVariable=TRUE"; ManagementObjectSearcher searcher = new ManagementObjectSearcher( "root\\CIMV2", query ); ManagementObjectCollection colItems = searcher.Get( ); foreach ( ManagementObject item in colItems ) { if ( verbose ) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine( "PATH={0}", item["VariableValue"] ); Console.ResetColor( ); Console.WriteLine( ); } PATH = item["VariableValue"].ToString( ).Split( ';' ); foreach ( string folder in PATH ) { if ( string.IsNullOrWhiteSpace( folder ) ) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine( "" ); Console.ResetColor( ); errors++; empty++; } else { if ( Directory.Exists( Environment.ExpandEnvironmentVariables( folder ) ) ) { fgcolor = ConsoleColor.Green; } else { fgcolor = ConsoleColor.Red; errors++; notexist++; } Console.ForegroundColor = fgcolor; Console.WriteLine( folder ); } } Console.ResetColor( ); } Console.WriteLine( ); #endregion System PATH #region User PATH Console.WriteLine( ); Console.WriteLine( "User PATH" ); Console.WriteLine( "=========" ); query = string.Format( @"SELECT * FROM Win32_Environment WHERE Name='PATH' AND UserName='{0}\\{1}'", Environment.GetEnvironmentVariable( "ComputerName" ), Environment.UserName ); searcher = new ManagementObjectSearcher( "root\\CIMV2", query ); colItems = searcher.Get( ); foreach ( ManagementObject item in colItems ) { if ( verbose ) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine( "PATH={0}", item["VariableValue"] ); Console.ResetColor( ); Console.WriteLine( ); } PATH = item["VariableValue"].ToString( ).Split( ';' ); foreach ( string folder in PATH ) { if ( string.IsNullOrWhiteSpace( folder ) ) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine( "" ); Console.ResetColor( ); errors++; empty++; } else { if ( Directory.Exists( Environment.ExpandEnvironmentVariables( folder ) ) ) { fgcolor = ConsoleColor.Green; } else { fgcolor = ConsoleColor.Red; errors++; notexist++; } Console.ForegroundColor = fgcolor; Console.WriteLine( folder ); } } Console.ResetColor( ); } Console.WriteLine( ); #endregion User PATH #region Error Warning if ( verbose) { if ( empty > 0) { fgcolor = ConsoleColor.Red; } else { fgcolor= ConsoleColor.Green; } Console.ForegroundColor = fgcolor; Console.Write( "\n{0} empty", empty ); Console.ResetColor( ); Console.Write( " and " ); if ( notexist > 0 ) { fgcolor = ConsoleColor.Red; } else { fgcolor = ConsoleColor.Green; } Console.ForegroundColor = fgcolor; Console.Write( "{0} non existing", notexist ); Console.ResetColor( ); Console.WriteLine( " PATH entries were found.\n" ); } if ( !quiet && errors > 0 ) { string message = string.Empty; if ( empty > 0 && notexist == 0 ) { message = string.Format( "{0} empty PATH entr{1} found.", empty, ( empty == 1 ? "y was" : "ies were" ) ); } else if ( empty == 0 && notexist > 0 ) { message = string.Format( "{0} non existing PATH entr{1} found.", notexist, ( notexist == 1 ? "y was" : "ies were" ) ); } else { message = string.Format( "{0} empty and {1} non existing PATH entries were found.", empty, notexist ); } message += "\n\nIn the \"System Properties\" window, \"Advanced\" tab, click the \"Environment "; message += string.Format( "Variables\" button and correct the error{0}", ( errors == 1 ? "" : "s" ) ); string title = string.Format( "Correct {0} error{1}", errors, ( errors == 1 ? "" : "s" ) ); DialogResult answer = MessageBox.Show( message, title, MessageBoxButtons.OKCancel, MessageBoxIcon.Error ); if ( answer == DialogResult.OK ) { try { Process.Start( "SystemPropertiesAdvanced.exe" ).WaitForExit( ); } catch ( Exception ) { // With RunDLL32 we don't WaitForExit, as it exits immediately after launching the Control Panel applet Process.Start( "RunDLL32.exe", "Shell32.dll,Control_RunDLL SYSDM.CPL,@0,3" ); } } } #endregion Error Warning return errors; } 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 /* CheckPath.exe, Version 1.00 Check if all directories in the PATH variable(s) really exist Usage: CheckPath.exe [ /Quiet ] [ /Verbose ] [ /? ] Where: /Quiet skips the popup message when errors are found /Verbose shows extra information, i.e. full PATH variables /? shows this help screen Notes: Each directory in the System PATH and the User PATH will be checked. If it exists, its name is displayed in green, otherwise in red. A counter keeps track of errors, i.e. empty PATH entries as well as directories that don't exist, and its count is used as the program's return code. Unless the /Quiet switch is used, a popup message will show the number of errors detected, if any, and explain how to correct this. If "OK" is clicked, the "System Properties" settings window's "Advanced" tab is opened: click its "Environment Variables" button and correct the errors. If "Cancel" is clicked, no settings window is opened, and the program terminates. Written by Rob van der Woude https://www.robvanderwoude.com */ Console.Error.WriteLine( ); Console.Error.WriteLine( "CheckPath.exe, Version {0}", progver ); Console.Error.WriteLine( "Check if all directories in the PATH variable(s) really exist" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "CheckPath.exe [ /Quiet ] [ /Verbose ] [ /? ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/Q" ); Console.ResetColor( ); Console.Error.WriteLine( "uiet skips the popup message when errors are found" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /V" ); Console.ResetColor( ); Console.Error.WriteLine( "erbose shows extra information, i.e. full PATH variables" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /?" ); Console.ResetColor( ); Console.Error.WriteLine( " shows this help screen" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: Each directory in the System PATH and the User PATH will be checked." ); Console.Error.Write( " If it exists, its name is displayed in " ); Console.ForegroundColor = ConsoleColor.Green; Console.Error.Write( "green" ); Console.ResetColor( ); Console.Error.Write( ", otherwise in " ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "red" ); Console.ResetColor( ); Console.Error.WriteLine( "." ); Console.Error.WriteLine( " A counter keeps track of errors, i.e. empty PATH entries as well as" ); Console.Error.WriteLine( " directories that don't exist, and its count is used as the program's" ); Console.Error.WriteLine( " return code." ); Console.Error.Write( " Unless the " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/Q" ); Console.ResetColor( ); Console.Error.WriteLine( "uiet switch is used, a popup message will show the number" ); Console.Error.WriteLine( " of errors detected, if any, and explain how to correct this." ); Console.Error.WriteLine( " If \"OK\" is clicked, the \"System Properties\" settings window's" ); Console.Error.WriteLine( " \"Advanced\" tab is opened: click its \"Environment Variables\" button and" ); Console.Error.WriteLine( " correct the errors. If \"Cancel\" is clicked, no settings window is" ); Console.Error.WriteLine( " opened, and the program terminates." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Help Text return -1; } } }