21

I want to get the list of removable disk in c#. I want to skip the local drives. Because i want the user to save the file only in removable disk.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
Anuya
  • 8,082
  • 49
  • 137
  • 222

3 Answers3

40

You will need to reference System.IO for this method.

var driveList = DriveInfo.GetDrives();

foreach (DriveInfo drive in driveList)
{
    if (drive .DriveType == DriveType.Removable)
    {
    //Add to RemovableDrive list or whatever activity you want
    }    
}

Or for the LINQ fans:

var driveList = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Removable);



Added
As for the Saving part, as far as I know I don't think you can restrict where the user is allowed to save to using a SaveFileDialog, but you could complete a check after you have shown the SaveFileDialog.

if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
  if (CheckFilePathIsOfRemovableDisk(saveFileDialog.FileName) == true)
  {
  //carry on with save
  }
  else
  {
  MessageBox.Show("Must save to Removable Disk, location was not valid");
  }
}

OR

The best option would be to create your own Save Dialog, which contains a tree view, only showing the removable drives and their contents for the user to save to! I would recommend this option.

Hope this helps

Shadrack Orina
  • 7,713
  • 2
  • 31
  • 36
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
  • Hi ThePower, I want to show the available removable drives to the user as a dialog box to save a file. Please help. – Anuya Jul 14 '09 at 10:19
  • 1
    @karthik I think you will have to make your own customised Save Dialog, which will only show the directories you got from the List of Drives. You could try setting the saveFileDialog.InitialDirectory to be one of the removable drives, but the user will still be able to return to the hard drive etc. If you have the time to create your own dialog, then that is the best option, using a TreeView to show the RemovableDrives and their contents, but this will be more time consuming than you are expecting to achieve this solution. – Lloyd Powell Jul 14 '09 at 10:26
  • There's an error in the linq query, it should be "Removable" instead of "Removeable" – Lonefish Aug 22 '16 at 14:43
11

How about:

var removableDrives = from d in System.IO.DriveInfo.GetDrives()
                      where d.DriveType == DriveType.Removable;
Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
  • 3
    Or the synonymous: var removableDrives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Removable); – Rhys Jones Jul 14 '09 at 10:06
5

You can also use WMI to get the list of removable drives.

ManagementObjectCollection drives = new ManagementObjectSearcher (
     "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();

Edited based on comment:

After you get the list of drives get there GUID's and add them to SaveFileDialogInstance.CustomPlaces collection.

The code below need some tweaking...

System.Windows.Forms.SaveFileDialog dls = new System.Windows.Forms.SaveFileDialog();
dls.CustomPlaces.Clear();
dls.CustomPlaces.Add(AddGuidOfTheExternalDriveOneByOne);
....
....
dls.ShowDialog();
S M Kamran
  • 4,423
  • 7
  • 25
  • 35
  • What if it's SD rather than USB? Or will this cater for that? – Lloyd Powell Jul 14 '09 at 10:10
  • 2
    Using WMI like suggested here is actyually a more solid way to get USB devices than `DriveInfo.GetDrives()` and relying on the `IsRemovable` property: harddrives plugged into the USB port may not be identified as `IsRemovable` so the are easy to miss. Using WMI requires a bit more work but is more robust. – Renaud Bompuis Apr 22 '19 at 08:37
  • how do you get the drive letter and name? – newbieguy Dec 27 '19 at 13:20