How can I discover any USB storage devices and/or CD/DVD writers available at a given time (using C# .Net2.0).
I would like to present users with a choice of devices onto which a file can be stored for physically removal - i.e. not the hard drive.
How can I discover any USB storage devices and/or CD/DVD writers available at a given time (using C# .Net2.0).
I would like to present users with a choice of devices onto which a file can be stored for physically removal - i.e. not the hard drive.
this is VB.NET code to check for any removable drives or CDRom drives attached to the computer:
Me.lstDrives.Items.Clear()
For Each item As DriveInfo In My.Computer.FileSystem.Drives
If item.DriveType = DriveType.Removable Or item.DriveType = DriveType.CDRom Then
Me.lstDrives.Items.Add(item.Name)
End If
Next
it won't be that hard to modify this code into a c# equivalent, and more driveType's are available.
From MSDN:
using System.IO;
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady && d.DriveType == DriveType.Removable)
{
// This is the drive you want...
}
}
The DriveInfo class documentation is here:
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
in c# you can get the same by using the System.IO.DriveInfo class
using System.IO;
public static class GetDrives
{
public static IEnumerable<DriveInfo> GetCDDVDAndRemovableDevices()
{
return DriveInfo.GetDrives().
Where(d => d.DriveType == DriveType.Removable
&& d.DriveType == DriveType.CDRom);
}
}