Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for checkpath.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Management;
  7. using System.Windows.Forms;
  8.  
  9.  
  10. namespace RobvanderWoude
  11. {
  12. 	internal class CheckPath
  13. 	{
  14. 		static string progver = "1.00";
  15.  
  16.  
  17. 		static int Main( string[] args )
  18. 		{
  19. 			#region Parse Command Line
  20.  
  21. 			// Only 3 valid arguments are checked, invalid arguments are ignored
  22. 			if ( args.Contains( "/?", StringComparer.InvariantCultureIgnoreCase ) )
  23. 			{
  24. 				return ShowHelp( );
  25. 			}
  26. 			bool quiet = args.Contains( "/quiet", StringComparer.InvariantCultureIgnoreCase ) || args.Contains( "/q", StringComparer.InvariantCultureIgnoreCase );
  27. 			bool verbose = args.Contains( "/verbose", StringComparer.InvariantCultureIgnoreCase ) || args.Contains( "/v", StringComparer.InvariantCultureIgnoreCase );
  28.  
  29. 			#endregion Parse Command Line
  30.  
  31.  
  32. 			#region Initialize Variables
  33.  
  34. 			ConsoleColor fgcolor;
  35. 			string[] PATH;
  36. 			int errors = 0;
  37. 			int notexist = 0;
  38. 			int empty = 0;
  39.  
  40. 			#endregion Initialize Variables
  41.  
  42.  
  43. 			#region System PATH
  44.  
  45. 			Console.WriteLine( );
  46. 			Console.WriteLine( "System PATH" );
  47. 			Console.WriteLine( "===========" );
  48. 			string query = @"SELECT * FROM Win32_Environment WHERE Name='PATH' AND SystemVariable=TRUE";
  49. 			ManagementObjectSearcher searcher = new ManagementObjectSearcher( "root\\CIMV2", query );
  50. 			ManagementObjectCollection colItems = searcher.Get( );
  51. 			foreach ( ManagementObject item in colItems )
  52. 			{
  53. 				if ( verbose )
  54. 				{
  55. 					Console.ForegroundColor = ConsoleColor.White;
  56. 					Console.WriteLine( "PATH={0}", item["VariableValue"] );
  57. 					Console.ResetColor( );
  58. 					Console.WriteLine( );
  59. 				}
  60. 				PATH = item["VariableValue"].ToString( ).Split( ';' );
  61. 				foreach ( string folder in PATH )
  62. 				{
  63. 					if ( string.IsNullOrWhiteSpace( folder ) )
  64. 					{
  65. 						Console.ForegroundColor = ConsoleColor.Red;
  66. 						Console.WriteLine( "<empty>" );
  67. 						Console.ResetColor( );
  68. 						errors++;
  69. 						empty++;
  70. 					}
  71. 					else
  72. 					{
  73. 						if ( Directory.Exists( Environment.ExpandEnvironmentVariables( folder ) ) )
  74. 						{
  75. 							fgcolor = ConsoleColor.Green;
  76. 						}
  77. 						else
  78. 						{
  79. 							fgcolor = ConsoleColor.Red;
  80. 							errors++;
  81. 							notexist++;
  82. 						}
  83. 						Console.ForegroundColor = fgcolor;
  84. 						Console.WriteLine( folder );
  85. 					}
  86. 				}
  87. 				Console.ResetColor( );
  88. 			}
  89. 			Console.WriteLine( );
  90.  
  91. 			#endregion System PATH
  92.  
  93.  
  94. 			#region User PATH
  95.  
  96. 			Console.WriteLine( );
  97. 			Console.WriteLine( "User PATH" );
  98. 			Console.WriteLine( "=========" );
  99. 			query = string.Format( @"SELECT * FROM Win32_Environment WHERE Name='PATH' AND UserName='{0}\\{1}'", Environment.GetEnvironmentVariable( "ComputerName" ), Environment.UserName );
  100. 			searcher = new ManagementObjectSearcher( "root\\CIMV2", query );
  101. 			colItems = searcher.Get( );
  102. 			foreach ( ManagementObject item in colItems )
  103. 			{
  104. 				if ( verbose )
  105. 				{
  106. 					Console.ForegroundColor = ConsoleColor.White;
  107. 					Console.WriteLine( "PATH={0}", item["VariableValue"] );
  108. 					Console.ResetColor( );
  109. 					Console.WriteLine( );
  110. 				}
  111. 				PATH = item["VariableValue"].ToString( ).Split( ';' );
  112. 				foreach ( string folder in PATH )
  113. 				{
  114. 					if ( string.IsNullOrWhiteSpace( folder ) )
  115. 					{
  116. 						Console.ForegroundColor = ConsoleColor.Red;
  117. 						Console.WriteLine( "<empty>" );
  118. 						Console.ResetColor( );
  119. 						errors++;
  120. 						empty++;
  121. 					}
  122. 					else
  123. 					{
  124. 						if ( Directory.Exists( Environment.ExpandEnvironmentVariables( folder ) ) )
  125. 						{
  126. 							fgcolor = ConsoleColor.Green;
  127. 						}
  128. 						else
  129. 						{
  130. 							fgcolor = ConsoleColor.Red;
  131. 							errors++;
  132. 							notexist++;
  133. 						}
  134. 						Console.ForegroundColor = fgcolor;
  135. 						Console.WriteLine( folder );
  136. 					}
  137. 				}
  138. 				Console.ResetColor( );
  139. 			}
  140. 			Console.WriteLine( );
  141.  
  142. 			#endregion User PATH
  143.  
  144.  
  145. 			#region Error Warning
  146.  
  147. 			if ( verbose)
  148. 			{
  149. 				if ( empty > 0)
  150. 				{
  151. 					fgcolor = ConsoleColor.Red;
  152. 				}
  153. 				else
  154. 				{
  155. 					fgcolor= ConsoleColor.Green;
  156. 				}
  157. 				Console.ForegroundColor = fgcolor;
  158. 				Console.Write( "\n{0} empty", empty );
  159. 				Console.ResetColor( );
  160. 				Console.Write( " and " );
  161. 				if ( notexist > 0 )
  162. 				{
  163. 					fgcolor = ConsoleColor.Red;
  164. 				}
  165. 				else
  166. 				{
  167. 					fgcolor = ConsoleColor.Green;
  168. 				}
  169. 				Console.ForegroundColor = fgcolor;
  170. 				Console.Write( "{0} non existing", notexist );
  171. 				Console.ResetColor( );
  172. 				Console.WriteLine( " PATH entries were found.\n" );
  173. 			}
  174.  
  175. 			if ( !quiet && errors > 0 )
  176. 			{
  177. 				string message = string.Empty;
  178. 				if ( empty > 0 && notexist == 0 )
  179. 				{
  180. 					message = string.Format( "{0} empty PATH entr{1} found.", empty, ( empty == 1 ? "y was" : "ies were" ) );
  181. 				}
  182. 				else if ( empty == 0 && notexist > 0 )
  183. 				{
  184. 					message = string.Format( "{0} non existing PATH entr{1} found.", notexist, ( notexist == 1 ? "y was" : "ies were" ) );
  185. 				}
  186. 				else
  187. 				{
  188. 					message = string.Format( "{0} empty and {1} non existing PATH entries were found.", empty, notexist );
  189. 				}
  190. 				message += "\n\nIn the \"System Properties\" window, \"Advanced\" tab, click the \"Environment ";
  191. 				message += string.Format( "Variables\" button and correct the error{0}", ( errors == 1 ? "" : "s" ) );
  192. 				string title = string.Format( "Correct {0} error{1}", errors, ( errors == 1 ? "" : "s" ) );
  193. 				DialogResult answer = MessageBox.Show( message, title, MessageBoxButtons.OKCancel, MessageBoxIcon.Error );
  194. 				if ( answer == DialogResult.OK )
  195. 				{
  196. 					try
  197. 					{
  198. 						Process.Start( "SystemPropertiesAdvanced.exe" ).WaitForExit( );
  199. 					}
  200. 					catch ( Exception )
  201. 					{
  202. 						// With RunDLL32 we don't WaitForExit, as it exits immediately after launching the Control Panel applet
  203. 						Process.Start( "RunDLL32.exe", "Shell32.dll,Control_RunDLL SYSDM.CPL,@0,3" );
  204. 					}
  205. 				}
  206. 			}
  207.  
  208. 			#endregion Error Warning
  209.  
  210.  
  211. 			return errors;
  212. 		}
  213.  
  214.  
  215. 		static int ShowHelp( params string[] errmsg )
  216. 		{
  217. 			#region Error Message
  218.  
  219. 			if ( errmsg.Length > 0 )
  220. 			{
  221. 				List<string> errargs = new List<string>( errmsg );
  222. 				errargs.RemoveAt( 0 );
  223. 				Console.Error.WriteLine( );
  224. 				Console.ForegroundColor = ConsoleColor.Red;
  225. 				Console.Error.Write( "ERROR:\t" );
  226. 				Console.ForegroundColor = ConsoleColor.White;
  227. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  228. 				Console.ResetColor( );
  229. 			}
  230.  
  231. 			#endregion Error Message
  232.  
  233. 			#region Help Text
  234.  
  235. 			/*
  236. 			CheckPath.exe,  Version 1.00
  237. 			Check if all directories in the PATH variable(s) really exist
  238.  
  239. 			Usage:    CheckPath.exe  [ /Quiet ]  [ /Verbose ]  [ /? ]
  240.  
  241. 			Where:    /Quiet     skips the popup message when errors are found
  242. 			          /Verbose   shows extra information, i.e. full PATH variables
  243. 			          /?         shows this help screen
  244.  
  245. 			Notes:    Each directory in the System PATH and the User PATH will be checked.
  246. 			          If it exists, its name is displayed in green, otherwise in red.
  247. 			          A counter keeps track of errors, i.e. empty PATH entries as well as
  248. 			          directories that don't exist, and its count is used as the program's
  249. 			          return code.
  250. 			          Unless the /Quiet switch is used, a popup message will show the number
  251. 			          of errors detected, if any, and explain how to correct this.
  252. 			          If "OK" is clicked, the "System Properties" settings window's
  253. 			          "Advanced" tab is opened: click its "Environment Variables" button and
  254. 			          correct the errors. If "Cancel" is clicked, no settings window is
  255. 			          opened, and the program terminates.
  256.  
  257. 			Written by Rob van der Woude
  258. 			https://www.robvanderwoude.com
  259. 			*/
  260.  
  261. 			Console.Error.WriteLine( );
  262.  
  263. 			Console.Error.WriteLine( "CheckPath.exe,  Version {0}", progver );
  264.  
  265. 			Console.Error.WriteLine( "Check if all directories in the PATH variable(s) really exist" );
  266.  
  267. 			Console.Error.WriteLine( );
  268.  
  269. 			Console.Error.Write( "Usage:    " );
  270. 			Console.ForegroundColor = ConsoleColor.White;
  271. 			Console.Error.WriteLine( "CheckPath.exe  [ /Quiet ]  [ /Verbose ]  [ /? ]" );
  272. 			Console.ResetColor( );
  273.  
  274. 			Console.Error.WriteLine( );
  275.  
  276. 			Console.Error.Write( "Where:    " );
  277. 			Console.ForegroundColor = ConsoleColor.White;
  278. 			Console.Error.Write( "/Q" );
  279. 			Console.ResetColor( );
  280. 			Console.Error.WriteLine( "uiet     skips the popup message when errors are found" );
  281.  
  282. 			Console.ForegroundColor = ConsoleColor.White;
  283. 			Console.Error.Write( "          /V" );
  284. 			Console.ResetColor( );
  285. 			Console.Error.WriteLine( "erbose   shows extra information, i.e. full PATH variables" );
  286.  
  287. 			Console.ForegroundColor = ConsoleColor.White;
  288. 			Console.Error.Write( "          /?" );
  289. 			Console.ResetColor( );
  290. 			Console.Error.WriteLine( "         shows this help screen" );
  291.  
  292. 			Console.Error.WriteLine( );
  293.  
  294. 			Console.Error.WriteLine( "Notes:    Each directory in the System PATH and the User PATH will be checked." );
  295.  
  296. 			Console.Error.Write( "          If it exists, its name is displayed in " );
  297. 			Console.ForegroundColor = ConsoleColor.Green;
  298. 			Console.Error.Write( "green" );
  299. 			Console.ResetColor( );
  300. 			Console.Error.Write( ", otherwise in " );
  301. 			Console.ForegroundColor = ConsoleColor.Red;
  302. 			Console.Error.Write( "red" );
  303. 			Console.ResetColor( );
  304. 			Console.Error.WriteLine( "." );
  305.  
  306. 			Console.Error.WriteLine( "          A counter keeps track of errors, i.e. empty PATH entries as well as" );
  307.  
  308. 			Console.Error.WriteLine( "          directories that don't exist, and its count is used as the program's" );
  309.  
  310. 			Console.Error.WriteLine( "          return code." );
  311.  
  312. 			Console.Error.Write( "          Unless the " );
  313. 			Console.ForegroundColor = ConsoleColor.White;
  314. 			Console.Error.Write( "/Q" );
  315. 			Console.ResetColor( );
  316. 			Console.Error.WriteLine( "uiet switch is used, a popup message will show the number" );
  317.  
  318. 			Console.Error.WriteLine( "          of errors detected, if any, and explain how to correct this." );
  319.  
  320. 			Console.Error.WriteLine( "          If \"OK\" is clicked, the \"System Properties\" settings window's" );
  321.  
  322. 			Console.Error.WriteLine( "          \"Advanced\" tab is opened: click its \"Environment Variables\" button and" );
  323.  
  324. 			Console.Error.WriteLine( "          correct the errors. If \"Cancel\" is clicked, no settings window is" );
  325.  
  326. 			Console.Error.WriteLine( "          opened, and the program terminates." );
  327.  
  328. 			Console.Error.WriteLine( );
  329.  
  330. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  331.  
  332. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  333.  
  334. 			#endregion Help Text
  335.  
  336. 			return -1;
  337. 		}
  338. 	}
  339. }
  340.  

page last modified: 2022-10-20