(view source code of listdrives.cs as plain text)
using System;
using System.Management;
using System.Collections.Generic;
using System.Linq;
namespace RobvanderWoude
{
public class ListDrives
{
static readonly string progver = "1.11";
public static int Main( string[] args )
{
string computer = string.Empty;
#region Parse Command Line
// Only 1 optional argument allowed: a remote computer name
if ( args.Length > 1 )
{
return ShowHelp( "Too many command line arguments" );
}
if ( args.Length == 1 )
{
// We'll display a 'friendly' message if help was requested
if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) )
{
switch ( args[0].ToUpper( ) )
{
case "/?":
case "-?":
case "/H":
case "-H":
case "--H":
case "/HELP":
case "-HELP":
case "--HELP":
return ShowHelp( string.Empty );
default:
return ShowHelp( "Invalid command line argument \"{0}\"", args[0] );
}
}
else
{
computer = "\\\\" + args[0] + "\\";
}
}
#endregion Parse Command Line
string wminamespace = computer + "root\\CIMV2";
string wmiquery = "SELECT * FROM Win32_LogicalDisk";
List<string> drives = new List<string>( );
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher( wminamespace, wmiquery );
foreach ( ManagementObject queryObj in searcher.Get( ) )
{
drives.Add( queryObj["DeviceID"].ToString( ) );
}
}
catch ( Exception e )
{
return ShowHelp( e.Message );
}
drives.Sort( );
Console.WriteLine( string.Join( " ", drives ) );
return 0;
}
static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Help Text
/*
ListDrives.exe, Version 1.11
List all drive letters in use on the specified computer
Usage: ListDrives.exe [ computername ]
Where: computername is the (optional) name of a remote computer
(default if not specified: local computer)
Note: Return code 1 in case of errors, otherwise 0.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "ListDrives.exe, Version {0}", progver );
Console.Error.WriteLine( "List all drive letters in use on the specified computer" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "ListDrives.exe [ computername ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Where: computername is the (optional) name of a remote computer" );
Console.Error.WriteLine( " (default if not specified: local computer)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Note: Return code 1 in case of errors, otherwise 0." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return 1;
}
}
}
page last modified: 2024-04-16; loaded in 0.0063 seconds