Rob van der Woude's Scripting Pages
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. using System.Management;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class Drives
  10. 	{
  11. 		static readonly string progver = "3.00";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			bool showused = true;
  17. 			bool showavailable = true;
  18. 			bool showtype = false;
  19. 			bool showNotready = true;
  20.  
  21. 			#region Command line parsing
  22.  
  23. 			// Only 1 optional argument allowed
  24. 			if ( args.Length > 1 )
  25. 			{
  26. 				return ShowHelp( "Too many command line arguments" );
  27. 			}
  28. 			if ( args.Length == 1 )
  29. 			{
  30. 				// We'll display a 'friendly' message if help was requested
  31. 				if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) )
  32. 				{
  33. 					switch ( args[0].ToUpper( ) )
  34. 					{
  35. 						case "/A":
  36. 						case "-A":
  37. 							showused = false;
  38. 							break;
  39. 						case "/T":
  40. 						case "-T":
  41. 							showtype = true;
  42. 							showavailable = false;
  43. 							break;
  44. 						case "/R":
  45. 						case "-R":
  46. 							showtype = true;
  47. 							showavailable = false;
  48. 							showNotready = false;
  49. 							break;
  50. 						case "/U":
  51. 						case "-U":
  52. 							showavailable = false;
  53. 							break;
  54. 						case "/?":
  55. 						case "-?":
  56. 						case "/H":
  57. 						case "-H":
  58. 						case "--H":
  59. 						case "/HELP":
  60. 						case "-HELP":
  61. 						case "--HELP":
  62. 							return ShowHelp( );
  63. 						default:
  64. 							return ShowHelp( "Invalid command line argument" );
  65. 					}
  66. 				}
  67. 				else
  68. 				{
  69. 					return ShowHelp( );
  70. 				}
  71. 			}
  72.  
  73. 			#endregion
  74.  
  75. 			// Based on code found at
  76. 			// http://www.dreamincode.net/code/snippet4795.htm
  77.  
  78.  
  79. 			if ( showavailable )
  80. 			{
  81. 				List<string> letters = new List<string>( );
  82.  
  83. 				// Get all avilable drive letters
  84. 				for ( int i = Convert.ToInt16( 'A' ); i < Convert.ToInt16( 'Z' ); i++ )
  85. 				{
  86. 					letters.Add( new string( new char[] { (char)i } ) );
  87. 				}
  88.  
  89. 				// Loop through each and remove it's drive letter from our list
  90. 				foreach ( DriveInfo drive in DriveInfo.GetDrives( ) )
  91. 				{
  92. 					letters.Remove( drive.Name.Substring( 0, 1 ).ToUpper( ) );
  93. 				}
  94.  
  95. 				// display the list
  96. 				if ( showused )
  97. 				{
  98. 					Console.Write( "Available : " );
  99. 				}
  100. 				foreach ( string letter in letters )
  101. 				{
  102. 					Console.Write( "{0}: ", letter );
  103. 				}
  104. 				Console.WriteLine( );
  105. 			}
  106.  
  107. 			if ( showused )
  108. 			{
  109. 				if ( showavailable )
  110. 				{
  111. 					Console.Write( "Used      : " );
  112. 				}
  113. 				foreach ( DriveInfo drive in DriveInfo.GetDrives( ) )
  114. 				{
  115. 					if ( showtype )
  116. 					{
  117. 						string outputtemplate = "{0,-4}    {1,-24}    {2}";
  118. 						bool isready = drive.IsReady;
  119. 						string driveletter = drive.Name.Substring( 0, 2 ).ToUpper( );
  120. 						if ( showNotready )
  121. 						{
  122. 							Console.WriteLine( outputtemplate, driveletter, GetDriveType( drive ), ( isready ? drive.DriveFormat : "-- not ready --" ) );
  123. 						}
  124. 						else
  125. 						{
  126. 							if ( isready )
  127. 							{
  128. 								Console.WriteLine( outputtemplate, driveletter, GetDriveType( drive ), drive.DriveFormat );
  129. 							}
  130. 						}
  131. 					}
  132. 					else
  133. 					{
  134. 						Console.Write( "{0} ", drive.Name.Substring( 0, 2 ).ToUpper( ) );
  135. 					}
  136. 				}
  137. 				Console.WriteLine( );
  138. 			}
  139.  
  140. 			return 0;
  141. 		}
  142.  
  143.  
  144. 		static string GetDriveType( DriveInfo drive )
  145. 		{
  146. 			string drivetype = drive.DriveType.ToString( );
  147. 			if ( drivetype == "Fixed" )
  148. 			{
  149. 				return string.Format( "Fixed {0,4:F0} GB", drive.TotalSize / 0x40000000 );
  150. 			}
  151. 			else if ( drivetype == "Removable" )
  152. 			{
  153. 				string driveletter = drive.Name.Substring( 0, 2 ).ToUpper( );
  154. 				Dictionary<int, string> mediatype = new Dictionary<int, string>( );
  155. 				mediatype[0] = "Unknown";
  156. 				mediatype[1] = "Floppy 5.25\" 1.2 MB";
  157. 				mediatype[2] = "Floppy 3.5\" 1.44 MB";
  158. 				mediatype[3] = "Floppy 3.5\" 2.88 MB";
  159. 				mediatype[4] = "Floppy 3.5\" 20.8 MB";
  160. 				mediatype[5] = "Floppy 3.5\" 720 KB";
  161. 				mediatype[6] = "Floppy 5.25\" 360 KB";
  162. 				mediatype[7] = "Floppy 5.25\" 320 KB";
  163. 				mediatype[8] = "Floppy 5.25\" 320 KB";
  164. 				mediatype[9] = "Floppy 5.25\" 180 KB";
  165. 				mediatype[10] = "Floppy 5.25\" 160 KB";
  166. 				mediatype[11] = "Removable media other than floppy";
  167. 				mediatype[12] = "Fixed hard disk media";
  168. 				mediatype[13] = "Floppy 3.5\" 120 MB";
  169. 				mediatype[14] = "Floppy 3.5\" 640 KB";
  170. 				mediatype[15] = "Floppy 5.25\" 640 KB";
  171. 				mediatype[16] = "Floppy 5.25\" 720 KB";
  172. 				mediatype[17] = "Floppy 3.5\" 1.2 MB";
  173. 				mediatype[18] = "Floppy 3.5\" 1.23 MB";
  174. 				mediatype[19] = "Floppy 5.25\" 1.23 MB";
  175. 				mediatype[20] = "Floppy 3.5\" 128 MB";
  176. 				mediatype[21] = "Floppy 3.5\" 230 MB";
  177. 				mediatype[22] = "Floppy 8\" 256 KB";
  178.  
  179. 				string query = string.Format( "SELECT * FROM Win32_LogicalDisk WHERE DeviceID = \"{0}\" AND MediaType IS NOT NULL", driveletter );
  180. 				ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\CIMV2", query );
  181. 				ManagementObjectCollection colDisks = searcher.Get( );
  182. 				foreach ( ManagementObject disk in colDisks )
  183. 				{
  184. 					int mtype = int.Parse( disk["MediaType"].ToString( ) );
  185. 					return mediatype[mtype];
  186. 				}
  187. 			}
  188. 			return drivetype;
  189. 		}
  190.  
  191.  
  192. 		public static int ShowHelp( params string[] errmsg )
  193. 		{
  194. 			#region Help Text
  195.  
  196. 			/*
  197. 			Drives.exe,  Version 3.00
  198. 			List available and/or used drive letters
  199.  
  200. 			Usage:    Drives.exe  [ options ]
  201.  
  202. 			Options:  /A         lists available drive letters only (default: all)
  203. 			          /R         skip drives that are not ready     (implies /T)
  204. 			          /T         display drive type and filesystem  (implies /U)
  205. 			          /U         lists used drive letters only      (default: all)
  206.  
  207. 			Written by Rob van der Woude
  208. 			http://www.robvanderwoude.com
  209. 			*/
  210.  
  211. 			#endregion Help Text
  212.  
  213.  
  214. 			#region Error Message
  215.  
  216. 			if ( errmsg.Length > 0 )
  217. 			{
  218. 				List<string> errargs = new List<string>( errmsg );
  219. 				errargs.RemoveAt( 0 );
  220. 				Console.Error.WriteLine( );
  221. 				Console.ForegroundColor = ConsoleColor.Red;
  222. 				Console.Error.Write( "ERROR:\t" );
  223. 				Console.ForegroundColor = ConsoleColor.White;
  224. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  225. 				Console.ResetColor( );
  226. 			}
  227.  
  228. 			#endregion Error Message
  229.  
  230.  
  231. 			#region Display Help Text
  232.  
  233. 			Console.Error.WriteLine( );
  234.  
  235. 			Console.Error.WriteLine( "Drives.exe,  Version {0}", progver );
  236.  
  237. 			Console.Error.WriteLine( "List available and/or used drive letters" );
  238.  
  239. 			Console.Error.WriteLine( );
  240.  
  241. 			Console.Error.Write( "Usage:    " );
  242. 			Console.ForegroundColor = ConsoleColor.White;
  243. 			Console.Error.WriteLine( "Drives.exe  [ options ]" );
  244.  
  245. 			Console.Error.WriteLine( );
  246.  
  247. 			Console.Error.Write( "Options:  " );
  248. 			Console.ForegroundColor = ConsoleColor.White;
  249. 			Console.Error.Write( "/A" );
  250. 			Console.ResetColor( );
  251. 			Console.Error.Write( "          lists " );
  252. 			Console.ForegroundColor = ConsoleColor.White;
  253. 			Console.Error.Write( "a" );
  254. 			Console.ResetColor( );
  255. 			Console.Error.WriteLine( "vailable drive letters only (default: all)" );
  256.  
  257. 			Console.ForegroundColor = ConsoleColor.White;
  258. 			Console.Error.Write( "          /R" );
  259. 			Console.ResetColor( );
  260. 			Console.Error.Write( "          skip drives that are not " );
  261. 			Console.ForegroundColor = ConsoleColor.White;
  262. 			Console.Error.Write( "r" );
  263. 			Console.ResetColor( );
  264. 			Console.Error.Write( "eady     (implies " );
  265. 			Console.ForegroundColor = ConsoleColor.White;
  266. 			Console.Error.Write( "/T" );
  267. 			Console.ResetColor( );
  268. 			Console.Error.WriteLine( ")" );
  269.  
  270. 			Console.ForegroundColor = ConsoleColor.White;
  271. 			Console.Error.Write( "          /T" );
  272. 			Console.ResetColor( );
  273. 			Console.Error.Write( "          display drive " );
  274. 			Console.ForegroundColor = ConsoleColor.White;
  275. 			Console.Error.Write( "t" );
  276. 			Console.ResetColor( );
  277. 			Console.Error.Write( "ype and filesystem  (implies " );
  278. 			Console.ForegroundColor = ConsoleColor.White;
  279. 			Console.Error.Write( "/U" );
  280. 			Console.ResetColor( );
  281. 			Console.Error.WriteLine( ")" );
  282.  
  283. 			Console.ForegroundColor = ConsoleColor.White;
  284. 			Console.Error.Write( "          /U" );
  285. 			Console.ResetColor( );
  286. 			Console.Error.Write( "          lists " );
  287. 			Console.ForegroundColor = ConsoleColor.White;
  288. 			Console.Error.Write( "u" );
  289. 			Console.ResetColor( );
  290. 			Console.Error.WriteLine( "sed drive letters only      (default: all)" );
  291.  
  292. 			Console.Error.WriteLine( );
  293.  
  294. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  295.  
  296. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  297.  
  298. 			#endregion Display Help Text
  299.  
  300.  
  301. 			return 1;
  302. 		}
  303. 	}
  304. }
  305.  

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