C# Checking for presence of usb drive
I am writing a file that extracts xml to obtain name of files and need to copy these files to the USB drive. The first 2 steps I able to do this. But questions is:
- How can I detect if there is a USB Drive
- Then detec开发者_开发知识库t which drive it is.
Thanks!
This code goes in the other direction, but it handles the "how do I find a USB drive" question:
using System.IO;
// . . .
foreach (DriveInfo removableDrive in DriveInfo.GetDrives().Where(
d => d.DriveType == DriveType.Removable && d.IsReady))
{
DirectoryInfo rootDirectory = removableDrive.RootDirectory;
string monitoredDirectory = Path.Combine(rootDirectory.FullName, DIRECTORY_TO_MONITOR);
string localDestDirectory = Path.Combine(destDirectory, removableDrive.VolumeLabel);
if (!Directory.Exists(localDestDirectory))
Directory.CreateDirectory(localDestDirectory);
if (Directory.Exists(monitoredDirectory))
{
foreach (string file in Directory.GetFiles(monitoredDirectory))
{
File.Copy(file, Path.Combine(localDestDirectory, Path.GetFileName(file)), true);
}
}
}
Check DriveInfo.GetDrives()
for DriveType.Removeable
property
then check FullName
精彩评论