views:

1352

answers:

4

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.

Thanks. Urgent

+2  A: 

This article looks to do the trick:

http://zayko.net/post/How-to-get-list-of-removable-drives-installed-on-a-computer-(C).aspx

Rhys Jones
H Rhys, I am new to iLists.How to execute the code in the link u gave, inorder to get show the dialog with removable drives to the user ?Please help. Urgent.
Anuya
The sample code at that link simply returns a list of the removable drives. To use this in a dialog, you will probably need to create a custom dialog as suggested by @ThePower.
Rhys Jones
+7  A: 

How about:

var removableDrives = from d in System.IO.DriveInfo.GetDrives()
                      where d.DriveType == DriveType.Removable;
Matt Hamilton
Or the synonymous: var removableDrives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Removable);
Rhys Jones
Good answer, if Linq is available to the user that is :-)
ThePower
+6  A: 
DriveInfo[] ListDrives = DriveInfo.GetDrives();

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



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

ThePower
Hi ThePower,I want to show the available removable drives to the user as a dialog box to save a file. Please help.
Anuya
@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.
ThePower
+2  A: 

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
What if it's SD rather than USB? Or will this cater for that?
ThePower
Hi ThePower, I want to show the available removable drives to the user as a dialog box to save a file. Please help.
Anuya