How to Check presence of a usb drive using C#?

Total Post:29


 2725  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.

  1. 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))
                if (Directory.Exists(monitoredDirectory))
                    foreach (string file in Directory.GetFiles(monitoredDirectory))
                        File.Copy(file, Path.Combine(localDestDirectory, Path.GetFileName(file)), true);

      Modified On Apr-03-2018 05:18:50 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.