Powered by GeSHi

Source code for listdrives.cs

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

  1. // The initial C# code for the "plain" WMI query was generated by
  2. // WMI Code Generator, Version 5.00, http://www.robvanderwoude.com/wmigen.php
  3.  
  4. using System;
  5. using System.Management;
  6. using System.Collections.Generic;
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	public class ListDrives
  11. 	{
  12. 		public static int Main( string[] args )
  13. 		{
  14. 			try
  15. 			{
  16. 				string computer = string.Empty;
  17.  
  18. 				#region Command line parsing
  19.  
  20. 				// Only 1 optional argument allowed: a remote computer name
  21. 				if ( args.Length > 1 )
  22. 				{
  23. 					throw new Exception( "Invalid 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 WriteError( string.Empty );
  41. 							default:
  42. 								return WriteError( "Invalid command line argument" );
  43. 						}
  44. 					}
  45. 					else
  46. 					{
  47. 						computer = "\\\\" + args[0] + "\\";
  48. 					}
  49. 				}
  50.  
  51. 				#endregion
  52.  
  53. 				string wmins = computer + "root\\CIMV2";
  54.  
  55. 				ManagementObjectSearcher searcher = new ManagementObjectSearcher( wmins, "SELECT * FROM Win32_LogicalDisk" );
  56.  
  57. 				List<string> drives = new List<string>( );
  58.  
  59. 				foreach ( ManagementObject queryObj in searcher.Get( ) )
  60. 				{
  61. 					drives.Add( queryObj["DeviceID"].ToString( ) );
  62. 				}
  63.  
  64. 				drives.Sort( );
  65.  
  66. 				string drivelist = "";
  67.  
  68. 				foreach ( string drive in drives )
  69. 				{
  70. 					drivelist += ( drive + " " );
  71. 				}
  72. 				Console.WriteLine( drivelist.Trim( ) );
  73.  
  74. 				return 0;
  75. 			}
  76. 			catch ( Exception e )
  77. 			{
  78. 				return WriteError( e );
  79. 			}
  80. 		}
  81.  
  82. 		public static int WriteError( Exception e )
  83. 		{
  84. 			return WriteError( e == null ? null : e.Message );
  85. 		}
  86.  
  87. 		public static int WriteError( string errorMessage )
  88. 		{
  89. 			/*
  90. 			ListDrives,  Version 1.10
  91. 			List all drive letters in use on the specified computer
  92.  
  93. 			Usage:  LISTDRIVES  [ computername ]
  94.  
  95. 			Where:  'computername'  is the (optional) name of a remote computer
  96. 									(default if not specified: local computer)
  97.  
  98. 			Written by Rob van der Woude
  99. 			http://www.robvanderwoude.com
  100. 			*/
  101.  
  102. 			string fullpath = Environment.GetCommandLineArgs( ).GetValue( 0 ).ToString( );
  103. 			string[] program = fullpath.Split( '\\' );
  104. 			string exename = program[program.GetUpperBound( 0 )];
  105. 			exename = exename.Substring( 0, exename.IndexOf( '.' ) );
  106.  
  107. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  108. 			{
  109. 				Console.Error.WriteLine( );
  110. 				Console.ForegroundColor = ConsoleColor.Red;
  111. 				Console.Error.Write( "ERROR:  " );
  112. 				Console.ForegroundColor = ConsoleColor.White;
  113. 				Console.Error.WriteLine( errorMessage );
  114. 				Console.ResetColor( );
  115. 			}
  116. 			Console.Error.WriteLine( );
  117. 			Console.Error.WriteLine( exename + ",  Version 1.10" );
  118. 			Console.Error.WriteLine( "List all drive letters in use on the specified computer" );
  119. 			Console.Error.WriteLine( );
  120. 			Console.Error.Write( "Usage:  " );
  121. 			Console.ForegroundColor = ConsoleColor.White;
  122. 			Console.Error.Write( exename.ToUpper( ) );
  123. 			Console.Error.WriteLine( "  [ computername ]" );
  124. 			Console.ResetColor( );
  125. 			Console.Error.WriteLine( );
  126. 			Console.Error.WriteLine( "Where:  'computername'  is the (optional) name of a remote computer" );
  127. 			Console.Error.WriteLine( "                        (default if not specified: local computer)" );
  128. 			Console.Error.WriteLine( );
  129. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  130. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  131. 			return 1;
  132. 		}
  133. 	}
  134. }
  135.