cd drive  
Author Message
ashk1860





PostPosted: Visual C# General, cd drive Top

hi

is there any way to undrestand that a drive is CD or Hard drive or ...

in general: is there any way to find out the type of a given drive in C#



Visual C#6  
 
 
timvw





PostPosted: Visual C# General, cd drive Top

Add a reference to System.Management.dll and:
        static void Main( string[] args )
        {
            string driveName = "c";

            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + driveName + ":\"");
            disk.Get();

            Console.WriteLine( "MediaType: " + GetMediaMeaning( (uint)disk["MediaType"] ) );
          
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        static string GetMediaMeaning( uint mediaType )
        {
            Dictionary<uint, string> mediaTypes = new Dictionary<uint, string>();
            mediaTypes.Add( 0, "Unknown Format" );
            mediaTypes.Add( 1, "51/4-Inch Floppy Disk - 1.2Mb - 512 bytes/sector" );
            mediaTypes.Add( 2, "31/2-Inch Floppy Disk - 1.44Mb -512 bytes/sector" );
            mediaTypes.Add( 3, "31/2-Inch Floppy Disk - 2.88Mb - 512 bytes/sector" );
            mediaTypes.Add( 4, "31/2-Inch Floppy Disk - 20.8Mb - 512 bytes/sector" );
            mediaTypes.Add( 5, "31/2-Inch Floppy Disk - 720Kb - 512 bytes/sector" );
            mediaTypes.Add( 6, "51/4-Inch Floppy Disk - 360Kb - 512 bytes/sector" );
            mediaTypes.Add( 7, "51/4-Inch Floppy Disk - 320Kb - 512 bytes/sector" );
            mediaTypes.Add( 8, "51/4-Inch Floppy Disk - 320Kb - 1024 bytes/sector" );
            mediaTypes.Add( 9, "51/4-Inch Floppy Disk - 180Kb - 512 bytes/sector" );
            mediaTypes.Add( 10, "51/4-Inch Floppy Disk - 160Kb - 512 bytes/sector" );
            mediaTypes.Add( 11, "Removable media other than floppy" );
            mediaTypes.Add( 12, "Fixed hard disk media" );
            mediaTypes.Add( 13, "31/2-Inch Floppy Disk - 120Mb - 512 bytes/sector" );
            mediaTypes.Add( 14, "31/2-Inch Floppy Disk - 640Kb - 512 bytes/sector" );
            mediaTypes.Add( 15, "51/4-Inch Floppy Disk - 640Kb - 512 bytes/sector" );
            mediaTypes.Add( 16, "51/4-Inch Floppy Disk - 720Kb - 512 bytes/sector" );
            mediaTypes.Add( 17, "31/2-Inch Floppy Disk - 1.2Mb - 512 bytes/secto" );
            mediaTypes.Add( 18, "31/2-Inch Floppy Disk - 1.23Mb - 1024 bytes/sector" );
            mediaTypes.Add( 19, "51/4-Inch Floppy Disk - 1.23Mb - 1024 bytes/sector" );
            mediaTypes.Add( 20, "31/2-Inch Floppy Disk - 128Mb - 512 bytes/sector" );
            mediaTypes.Add( 21, "31/2-Inch Floppy Disk - 230Mb - 512 bytes/sector" );
            mediaTypes.Add( 22, "8-Inch Floppy Disk - 256Kb - 128 bytes/sector" );

            string meaning = "Unknown format";
            mediaTypes.TryGetValue( mediaType, out meaning );
            return meaning;
        }


 
 
ahmedilyas





PostPosted: Visual C# General, cd drive Top

yes there is. you could use the System.IO.DriveInfo class in .NET 2.0:

DriveInfo[] theDrives = DriveInfo.GetDrives();

foreach(DriveInfo curDrive in theDrives)

{

if (curDrive.IsReady)

{

MessageBox.Show(curDrive.Name + Environment.NewLine + "is a type of: " + curDrive.DriveType.ToString());

}

}



 
 
ashk1860





PostPosted: Visual C# General, cd drive Top

hi

thank you for your swift response. I have not tried timvw's solution yet but ahmedilyas solution is working nice.

note that I have 3 network drives with password. and GetDrives() will just list network drives which I logged in before not all network drives.