Wednesday, December 3, 2008

Get Logical disk drives using C# code

This is the sample code to get the disk drives, cd, removable disk etc...

private void PopulateDriveList()
{
const int Removable = 2;
const int LocalDisk = 3;
const int Network = 4;
const int CD = 5;
//const int RAMDrive = 6;

//Get Drive list

ManagementObjectCollection queryCollection = getDrives();
foreach ( ManagementObject mo in queryCollection)
{
switch (int.Parse( mo["DriveType"].ToString()))
{
case Removable: //removable drives

break;
case LocalDisk: //Local drives

break;
case CD: //CD rom drives

break;
case Network: //Network drives

break;
default: //defalut to folder

break;
}
}

}
protected ManagementObjectCollection getDrives()
{
//get drive collection
ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
return queryCollection;
}

No comments: