Home:ALL Converter>How to discover USB storage devices and writable CD/DVD drives (C#)

How to discover USB storage devices and writable CD/DVD drives (C#)

Ask Time:2008-09-09T19:28:30         Author:Stuart Helwig

Json Formatter

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.

Author:Stuart Helwig,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51645/how-to-discover-usb-storage-devices-and-writable-cd-dvd-drives-c
Mark Ingram :

using System.IO;\n\nDriveInfo[] allDrives = DriveInfo.GetDrives();\nforeach (DriveInfo d in allDrives)\n{\n if (d.IsReady && d.DriveType == DriveType.Removable)\n {\n // This is the drive you want...\n }\n}\n\n\nThe DriveInfo class documentation is here:\n\nhttp://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx",
2008-09-09T11:46:05
sven :

this is VB.NET code to check for any removable drives or CDRom drives attached to the computer:\n\nMe.lstDrives.Items.Clear()\nFor Each item As DriveInfo In My.Computer.FileSystem.Drives\n If item.DriveType = DriveType.Removable Or item.DriveType = DriveType.CDRom Then\n Me.lstDrives.Items.Add(item.Name)\n End If\nNext\n\n\nit won't be that hard to modify this code into a c# equivalent, and more driveType's are available.\nFrom MSDN:\n\n\nUnknown: The type of drive is unknown. \nNoRootDirectory: The drive does not have a root directory.\nRemovable: The drive is a removable storage device, such as a floppy disk drive or a USB flash drive.\nFixed: The drive is a fixed disk.\nNetwork: The drive is a network drive.\nCDRom: The drive is an optical disc device, such as a CD or DVD-ROM. \nRam: The drive is a RAM disk.\n",
2008-09-09T11:35:44
Hath :

in c# you can get the same by using the System.IO.DriveInfo class\n\nusing System.IO;\n\npublic static class GetDrives\n{\n public static IEnumerable<DriveInfo> GetCDDVDAndRemovableDevices()\n {\n return DriveInfo.GetDrives().\n Where(d => d.DriveType == DriveType.Removable\n && d.DriveType == DriveType.CDRom);\n }\n\n}\n",
2008-09-09T11:47:39
yy