开发者

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:

  1. How can I detect if there is a USB Drive
  2. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜