(view source code of drives.cs as plain text)
using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
namespace RobvanderWoude
{
class Drives
{
static readonly string progver = "3.00";
static int Main( string[] args )
{
bool showused = true;
bool showavailable = true;
bool showtype = false;
bool showNotready = true;
#region Command line parsing
// Only 1 optional argument allowed
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 "/A":
case "-A":
showused = false;
break;
case "/T":
case "-T":
showtype = true;
showavailable = false;
break;
case "/R":
case "-R":
showtype = true;
showavailable = false;
showNotready = false;
break;
case "/U":
case "-U":
showavailable = false;
break;
case "/?":
case "-?":
case "/H":
case "-H":
case "--H":
case "/HELP":
case "-HELP":
case "--HELP":
return ShowHelp( );
default:
return ShowHelp( "Invalid command line argument" );
}
}
else
{
return ShowHelp( );
}
}
#endregion
// Based on code found at
// http://www.dreamincode.net/code/snippet4795.htm
if ( showavailable )
{
List<string> letters = new List<string>( );
// Get all avilable drive letters
for ( int i = Convert.ToInt16( 'A' ); i < Convert.ToInt16( 'Z' ); i++ )
{
letters.Add( new string( new char[] { (char)i } ) );
}
// Loop through each and remove it's drive letter from our list
foreach ( DriveInfo drive in DriveInfo.GetDrives( ) )
{
letters.Remove( drive.Name.Substring( 0, 1 ).ToUpper( ) );
}
// display the list
if ( showused )
{
Console.Write( "Available : " );
}
foreach ( string letter in letters )
{
Console.Write( "{0}: ", letter );
}
Console.WriteLine( );
}
if ( showused )
{
if ( showavailable )
{
Console.Write( "Used : " );
}
foreach ( DriveInfo drive in DriveInfo.GetDrives( ) )
{
if ( showtype )
{
string outputtemplate = "{0,-4} {1,-24} {2}";
bool isready = drive.IsReady;
string driveletter = drive.Name.Substring( 0, 2 ).ToUpper( );
if ( showNotready )
{
Console.WriteLine( outputtemplate, driveletter, GetDriveType( drive ), ( isready ? drive.DriveFormat : "-- not ready --" ) );
}
else
{
if ( isready )
{
Console.WriteLine( outputtemplate, driveletter, GetDriveType( drive ), drive.DriveFormat );
}
}
}
else
{
Console.Write( "{0} ", drive.Name.Substring( 0, 2 ).ToUpper( ) );
}
}
Console.WriteLine( );
}
return 0;
}
static string GetDriveType( DriveInfo drive )
{
string drivetype = drive.DriveType.ToString( );
if ( drivetype == "Fixed" )
{
return string.Format( "Fixed {0,4:F0} GB", drive.TotalSize / 0x40000000 );
}
else if ( drivetype == "Removable" )
{
string driveletter = drive.Name.Substring( 0, 2 ).ToUpper( );
Dictionary<int, string> mediatype = new Dictionary<int, string>( );
mediatype[0] = "Unknown";
mediatype[1] = "Floppy 5.25\" 1.2 MB";
mediatype[2] = "Floppy 3.5\" 1.44 MB";
mediatype[3] = "Floppy 3.5\" 2.88 MB";
mediatype[4] = "Floppy 3.5\" 20.8 MB";
mediatype[5] = "Floppy 3.5\" 720 KB";
mediatype[6] = "Floppy 5.25\" 360 KB";
mediatype[7] = "Floppy 5.25\" 320 KB";
mediatype[8] = "Floppy 5.25\" 320 KB";
mediatype[9] = "Floppy 5.25\" 180 KB";
mediatype[10] = "Floppy 5.25\" 160 KB";
mediatype[11] = "Removable media other than floppy";
mediatype[12] = "Fixed hard disk media";
mediatype[13] = "Floppy 3.5\" 120 MB";
mediatype[14] = "Floppy 3.5\" 640 KB";
mediatype[15] = "Floppy 5.25\" 640 KB";
mediatype[16] = "Floppy 5.25\" 720 KB";
mediatype[17] = "Floppy 3.5\" 1.2 MB";
mediatype[18] = "Floppy 3.5\" 1.23 MB";
mediatype[19] = "Floppy 5.25\" 1.23 MB";
mediatype[20] = "Floppy 3.5\" 128 MB";
mediatype[21] = "Floppy 3.5\" 230 MB";
mediatype[22] = "Floppy 8\" 256 KB";
string query = string.Format( "SELECT * FROM Win32_LogicalDisk WHERE DeviceID = \"{0}\" AND MediaType IS NOT NULL", driveletter );
ManagementObjectSearcher searcher = new ManagementObjectSearcher( @"root\CIMV2", query );
ManagementObjectCollection colDisks = searcher.Get( );
foreach ( ManagementObject disk in colDisks )
{
int mtype = int.Parse( disk["MediaType"].ToString( ) );
return mediatype[mtype];
}
}
return drivetype;
}
public static int ShowHelp( params string[] errmsg )
{
#region Help Text
/*
Drives.exe, Version 3.00
List available and/or used drive letters
Usage: Drives.exe [ options ]
Options: /A lists available drive letters only (default: all)
/R skip drives that are not ready (implies /T)
/T display drive type and filesystem (implies /U)
/U lists used drive letters only (default: all)
Written by Rob van der Woude
http://www.robvanderwoude.com
*/
#endregion Help Text
#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 Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "Drives.exe, Version {0}", progver );
Console.Error.WriteLine( "List available and/or used drive letters" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "Drives.exe [ options ]" );
Console.Error.WriteLine( );
Console.Error.Write( "Options: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/A" );
Console.ResetColor( );
Console.Error.Write( " lists " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "a" );
Console.ResetColor( );
Console.Error.WriteLine( "vailable drive letters only (default: all)" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /R" );
Console.ResetColor( );
Console.Error.Write( " skip drives that are not " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "r" );
Console.ResetColor( );
Console.Error.Write( "eady (implies " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/T" );
Console.ResetColor( );
Console.Error.WriteLine( ")" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /T" );
Console.ResetColor( );
Console.Error.Write( " display drive " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "t" );
Console.ResetColor( );
Console.Error.Write( "ype and filesystem (implies " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "/U" );
Console.ResetColor( );
Console.Error.WriteLine( ")" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /U" );
Console.ResetColor( );
Console.Error.Write( " lists " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "u" );
Console.ResetColor( );
Console.Error.WriteLine( "sed drive letters only (default: all)" );
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.0113 seconds