Home > DeveloperSection > Forums > How to Check presence of a usb drive using C#?
Andrew Deniel
Andrew Deniel

Total Post:29

Points:203
Posted on    October-16-2013 7:17 AM

 C# C# 
Ratings:


 1 Reply(s)
 1554  View(s)
Rate this:

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.

my questions is:

1.    How can I detect if there is a USB Drive

2.    Then detect which drive it is.

Thanks


Brad Pitt
Brad Pitt

Total Post:61

Points:429
Posted on    October-16-2013 7:49 AM

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);
                }
            }
        }

Don't want to miss updates? Please click the below button!

Follow MindStick