我正在编写一个文件,它提取xml以获取文件名,并需要将这些文件复制到USB驱动器。前两个步骤我能够做到这一点。但问题是:
谢谢!
答案 0 :(得分:5)
此代码向另一个方向发展,但它处理“我如何找到USB驱动器”的问题:
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);
}
}
}
答案 1 :(得分:1)
检查DriveInfo.GetDrives()
DriveType.Removeable
属性
然后检查FullName