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.
c) Click on the "Computer" utility (found in the "Start" menu) and then find your flash drive in the list of "Devices with Removable Storage." The USB flash drive should be named "Removable Disk." Double-click on it to open the flash drive's contents in a new window.
Removable media is any type of storage device that can be removed from a computer while the system is running. Examples of removable media include CDs, DVDs and Blu-Ray disks, as well as diskettes and USB drives.
Removable disks are alternatives to the internal hard drive for reading and storing data. Examples include USB drives, CD/DVD-ROM/RWs and external hard drives. These drives allow data to be accessed from external and mobile sources.
Usually, the Removable Storage Devices folder appears on your desktop after using certain external storage or after editing photos. It's a ghost folder that can't be deleted by simply refreshing your personal computer. The registry can also be a cause, so you should try to use a Windows 10 built-in tool to fix it.
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
How about:
var removableDrives = from d in System.IO.DriveInfo.GetDrives()
where d.DriveType == DriveType.Removable;
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With