Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listdrives.cs

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

  1. using System;
  2. using System.Management;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace RobvanderWoude
  7. {
  8. 	public class ListDrives
  9. 	{
  10. 		static readonly string progver = "1.11";
  11.  
  12.  
  13. 		public static int Main( string[] args )
  14. 		{
  15. 			string computer = string.Empty;
  16.  
  17.  
  18. 			#region Parse Command Line
  19.  
  20. 			// Only 1 optional argument allowed: a remote computer name
  21. 			if ( args.Length > 1 )
  22. 			{
  23. 				return ShowHelp( "Too many command line arguments" );
  24. 			}
  25. 			if ( args.Length == 1 )
  26. 			{
  27. 				// We'll display a 'friendly' message if help was requested
  28. 				if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) )
  29. 				{
  30. 					switch ( args[0].ToUpper( ) )
  31. 					{
  32. 						case "/?":
  33. 						case "-?":
  34. 						case "/H":
  35. 						case "-H":
  36. 						case "--H":
  37. 						case "/HELP":
  38. 						case "-HELP":
  39. 						case "--HELP":
  40. 							return ShowHelp( string.Empty );
  41. 						default:
  42. 							return ShowHelp( "Invalid command line argument \"{0}\"", args[0] );
  43. 					}
  44. 				}
  45. 				else
  46. 				{
  47. 					computer = "\\\\" + args[0] + "\\";
  48. 				}
  49. 			}
  50.  
  51. 			#endregion Parse Command Line
  52.  
  53.  
  54. 			string wminamespace = computer + "root\\CIMV2";
  55. 			string wmiquery = "SELECT * FROM Win32_LogicalDisk";
  56. 			List<string> drives = new List<string>( );
  57.  
  58. 			try
  59. 			{
  60. 				ManagementObjectSearcher searcher = new ManagementObjectSearcher( wminamespace, wmiquery );
  61. 				foreach ( ManagementObject queryObj in searcher.Get( ) )
  62. 				{
  63. 					drives.Add( queryObj["DeviceID"].ToString( ) );
  64. 				}
  65. 			}
  66. 			catch ( Exception e )
  67. 			{
  68. 				return ShowHelp( e.Message );
  69. 			}
  70.  
  71. 			drives.Sort( );
  72.  
  73. 			Console.WriteLine( string.Join( " ", drives ) );
  74.  
  75. 			return 0;
  76. 		}
  77.  
  78.  
  79. 		static int ShowHelp( params string[] errmsg )
  80. 		{
  81. 			#region Error Message
  82.  
  83. 			if ( errmsg.Length > 0 )
  84. 			{
  85. 				List<string> errargs = new List<string>( errmsg );
  86. 				errargs.RemoveAt( 0 );
  87. 				Console.Error.WriteLine( );
  88. 				Console.ForegroundColor = ConsoleColor.Red;
  89. 				Console.Error.Write( "ERROR:\t" );
  90. 				Console.ForegroundColor = ConsoleColor.White;
  91. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  92. 				Console.ResetColor( );
  93. 			}
  94.  
  95. 			#endregion Error Message
  96.  
  97.  
  98. 			#region Help Text
  99.  
  100. 			/*
  101. 			ListDrives.exe,  Version 1.11
  102. 			List all drive letters in use on the specified computer
  103.  
  104. 			Usage:  ListDrives.exe  [ computername ]
  105.  
  106. 			Where:  computername    is the (optional) name of a remote computer
  107. 									(default if not specified: local computer)
  108.  
  109. 			Note:   Return code 1 in case of errors, otherwise 0.
  110.  
  111. 			Written by Rob van der Woude
  112. 			https://www.robvanderwoude.com
  113. 			*/
  114.  
  115. 			#endregion Help Text
  116.  
  117.  
  118. 			#region Display Help Text
  119.  
  120. 			Console.Error.WriteLine( );
  121.  
  122. 			Console.Error.WriteLine( "ListDrives.exe,  Version {0}", progver );
  123.  
  124. 			Console.Error.WriteLine( "List all drive letters in use on the specified computer" );
  125.  
  126. 			Console.Error.WriteLine( );
  127.  
  128. 			Console.Error.Write( "Usage:  " );
  129. 			Console.ForegroundColor = ConsoleColor.White;
  130. 			Console.Error.WriteLine( "ListDrives.exe  [ computername ]" );
  131. 			Console.ResetColor( );
  132.  
  133. 			Console.Error.WriteLine( );
  134.  
  135. 			Console.Error.WriteLine( "Where:  computername    is the (optional) name of a remote computer" );
  136.  
  137. 			Console.Error.WriteLine( "                        (default if not specified: local computer)" );
  138.  
  139. 			Console.Error.WriteLine( );
  140.  
  141. 			Console.Error.WriteLine( "Note:   Return code 1 in case of errors, otherwise 0." );
  142.  
  143. 			Console.Error.WriteLine( );
  144.  
  145. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  146.  
  147. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  148.  
  149. 			#endregion Display Help Text
  150.  
  151.  
  152. 			return 1;
  153. 		}
  154. 	}
  155. }

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