HOW TO CHECK PRESENCE OF A USB DRIVE USING C#?

Andrew Deniel

Total Post:29

Points:203
Posted by  Andrew Deniel
C# 
C#
 2099  View(s)
Ratings:
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
  1. Brad Pitt

    Post:61

    Points:429
    Re: How to Check presence of a usb drive using C#?

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!