Powered by GeSHi

Source code for drives.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class Drives
  8. 	{
  9. 		static int Main( string[] args )
  10. 		{
  11. 			bool showused = true;
  12. 			bool showavailable = true;
  13. 			bool showtype = false;
  14. 			bool showNotready = true;
  15.  
  16. 			#region Command line parsing
  17.  
  18. 			// Only 1 optional argument allowed
  19. 			if ( args.Length > 1 )
  20. 			{
  21. 				return WriteError( "Too many command line arguments" );
  22. 			}
  23. 			if ( args.Length == 1 )
  24. 			{
  25. 				// We'll display a 'friendly' message if help was requested
  26. 				if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) )
  27. 				{
  28. 					switch ( args[0].ToUpper( ) )
  29. 					{
  30. 						case "/A":
  31. 						case "-A":
  32. 							showused = false;
  33. 							break;
  34. 						case "/T":
  35. 						case "-T":
  36. 							showtype = true;
  37. 							showavailable = false;
  38. 							break;
  39. 						case "/R":
  40. 						case "-R":
  41. 							showtype = true;
  42. 							showavailable = false;
  43. 							showNotready = false;
  44. 							break;
  45. 						case "/U":
  46. 						case "-U":
  47. 							showavailable = false;
  48. 							break;
  49. 						case "/?":
  50. 						case "-?":
  51. 						case "/H":
  52. 						case "-H":
  53. 						case "--H":
  54. 						case "/HELP":
  55. 						case "-HELP":
  56. 						case "--HELP":
  57. 							return WriteError( string.Empty );
  58. 						default:
  59. 							return WriteError( "Invalid command line argument" );
  60. 					}
  61. 				}
  62. 				else
  63. 				{
  64. 					return WriteError( string.Empty );
  65. 				}
  66. 			}
  67.  
  68. 			#endregion
  69.  
  70. 			// Based on code found at
  71. 			// http://www.dreamincode.net/code/snippet4795.htm
  72.  
  73.  
  74. 			if ( showavailable )
  75. 			{
  76. 				List<string> letters = new List<string>( );
  77.  
  78. 				// Get all avilable drive letters
  79. 				for ( int i = Convert.ToInt16( 'A' ); i < Convert.ToInt16( 'Z' ); i++ )
  80. 				{
  81. 					letters.Add( new string( new char[] { (char) i } ) );
  82. 				}
  83.  
  84. 				// Loop through each and remove it's drive letter from our list
  85. 				foreach ( DriveInfo drive in DriveInfo.GetDrives( ) )
  86. 				{
  87. 					letters.Remove( drive.Name.Substring( 0, 1 ).ToUpper( ) );
  88. 				}
  89.  
  90. 				// display the list
  91. 				if ( showused )
  92. 				{
  93. 					Console.Write( "Available : " );
  94. 				}
  95. 				foreach ( string letter in letters )
  96. 				{
  97. 					Console.Write( "{0}: ", letter );
  98. 				}
  99. 				Console.WriteLine( );
  100. 			}
  101.  
  102. 			if ( showused )
  103. 			{
  104. 				if ( showavailable )
  105. 				{
  106. 					Console.Write( "Used      : " );
  107. 				}
  108. 				foreach ( DriveInfo drive in DriveInfo.GetDrives( ) )
  109. 				{
  110. 					if ( showtype )
  111. 					{
  112. 						bool isready = drive.IsReady;
  113. 						if ( showNotready )
  114. 						{
  115. 							Console.WriteLine( "{0}\t{1,-12}\t{2}", drive.Name.Substring( 0, 2 ).ToUpper( ), drive.DriveType, ( isready ? drive.DriveFormat : "-- not ready --" ) );
  116. 						}
  117. 						else
  118. 						{
  119. 							if ( isready )
  120. 							{
  121. 								Console.WriteLine( "{0}\t{1,-12}\t{2}", drive.Name.Substring( 0, 2 ).ToUpper( ), drive.DriveType, drive.DriveFormat );
  122. 							}
  123. 						}
  124. 					}
  125. 					else
  126. 					{
  127. 						Console.Write( "{0} ", drive.Name.Substring( 0, 2 ).ToUpper( ) );
  128. 					}
  129. 				}
  130. 				Console.WriteLine( );
  131. 			}
  132.  
  133. 			return 0;
  134. 		}
  135.  
  136. 		public static int WriteError( Exception e )
  137. 		{
  138. 			return WriteError( e == null ? null : e.Message );
  139. 		}
  140.  
  141. 		public static int WriteError( string errorMessage )
  142. 		{
  143. 			/*
  144. 			Drives,  Version 2.00
  145. 			List available and/or used drive letters
  146.  
  147. 			Usage:  DRIVES  [ /A | /R | /T | /U ]
  148.  
  149. 			Where:  /A      lists available drive letters only (default: all)
  150. 			        /R      skip drives that are not ready     (implies /T)
  151. 			        /T      display drive type and filesystem  (implies /U)
  152. 			        /U      lists used drive letters only      (default: all)
  153.  
  154. 			Written by Rob van der Woude
  155. 			http://www.robvanderwoude.com
  156. 			*/
  157.  
  158. 			string fullpath = Environment.GetCommandLineArgs( ).GetValue( 0 ).ToString( );
  159. 			string[] program = fullpath.Split( '\\' );
  160. 			string exename = program[program.GetUpperBound( 0 )];
  161. 			exename = exename.Substring( 0, exename.IndexOf( '.' ) );
  162.  
  163. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  164. 			{
  165. 				Console.Error.WriteLine( );
  166. 				Console.ForegroundColor = ConsoleColor.Red;
  167. 				Console.Error.Write( "ERROR:  " );
  168. 				Console.ForegroundColor = ConsoleColor.White;
  169. 				Console.Error.WriteLine( errorMessage );
  170. 				Console.ResetColor( );
  171. 			}
  172. 			Console.Error.WriteLine( );
  173. 			Console.Error.WriteLine( exename + ",  Version 2.00" );
  174. 			Console.Error.WriteLine( "List available and/or used drive letters" );
  175. 			Console.Error.WriteLine( );
  176. 			Console.Error.Write( "Usage:  " );
  177. 			Console.ForegroundColor = ConsoleColor.White;
  178. 			Console.Error.Write( exename.ToUpper( ) );
  179. 			Console.ResetColor( );
  180. 			Console.Error.Write( "  [ " );
  181. 			Console.ForegroundColor = ConsoleColor.White;
  182. 			Console.Error.Write( "/A" );
  183. 			Console.ResetColor( );
  184. 			Console.Error.Write( " | " );
  185. 			Console.ForegroundColor = ConsoleColor.White;
  186. 			Console.Error.Write( "/R" );
  187. 			Console.ResetColor( );
  188. 			Console.Error.Write( " | " );
  189. 			Console.ForegroundColor = ConsoleColor.White;
  190. 			Console.Error.Write( "/T" );
  191. 			Console.ResetColor( );
  192. 			Console.Error.Write( " | " );
  193. 			Console.ForegroundColor = ConsoleColor.White;
  194. 			Console.Error.Write( "/U" );
  195. 			Console.ResetColor( );
  196. 			Console.Error.WriteLine( " ]" );
  197. 			Console.Error.WriteLine( );
  198. 			Console.Error.Write( "Where:  " );
  199. 			Console.ForegroundColor = ConsoleColor.White;
  200. 			Console.Error.Write( "/A" );
  201. 			Console.ResetColor( );
  202. 			Console.Error.Write( "      lists " );
  203. 			Console.ForegroundColor = ConsoleColor.White;
  204. 			Console.Error.Write( "a" );
  205. 			Console.ResetColor( );
  206. 			Console.Error.WriteLine( "vailable drive letters only (default: all)" );
  207. 			Console.ForegroundColor = ConsoleColor.White;
  208. 			Console.Error.Write( "        /R" );
  209. 			Console.ResetColor( );
  210. 			Console.Error.Write( "      skip drives that are not " );
  211. 			Console.ForegroundColor = ConsoleColor.White;
  212. 			Console.Error.Write( "r" );
  213. 			Console.ResetColor( );
  214. 			Console.Error.Write( "eady     (implies " );
  215. 			Console.ForegroundColor = ConsoleColor.White;
  216. 			Console.Error.Write( "/T" );
  217. 			Console.ResetColor( );
  218. 			Console.Error.WriteLine( ")" );
  219. 			Console.ForegroundColor = ConsoleColor.White;
  220. 			Console.Error.Write( "        /T" );
  221. 			Console.ResetColor( );
  222. 			Console.Error.Write( "      display drive " );
  223. 			Console.ForegroundColor = ConsoleColor.White;
  224. 			Console.Error.Write( "t" );
  225. 			Console.ResetColor( );
  226. 			Console.Error.Write( "ype and filesystem  (implies " );
  227. 			Console.ForegroundColor = ConsoleColor.White;
  228. 			Console.Error.Write( "/U" );
  229. 			Console.ResetColor( );
  230. 			Console.Error.WriteLine( ")" );
  231. 			Console.ForegroundColor = ConsoleColor.White;
  232. 			Console.Error.Write( "        /U" );
  233. 			Console.ResetColor( );
  234. 			Console.Error.Write( "      lists " );
  235. 			Console.ForegroundColor = ConsoleColor.White;
  236. 			Console.Error.Write( "u" );
  237. 			Console.ResetColor( );
  238. 			Console.Error.WriteLine( "sed drive letters only      (default: all)" );
  239. 			Console.Error.WriteLine( );
  240. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  241. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  242. 			return 1;
  243. 		}
  244. 	}
  245. }
  246.