C # Checking for a USB drive

I am writing a file that extracts xml to get the name of the files and should copy these files to a USB drive. The first two steps I can do this. But the questions are:

  • How to determine if there is a USB drive
  • Then determine which drive it is.

Thank!

+3
source share
2 answers

This code goes in a different direction, but it handles the question "how to find a USB drive":

 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);
                }
            }
        }
+5
source

Check DriveInfo.GetDrives()for DriveType.Removeableproperty then checkFullName

+1
source

All Articles