如何修改要复制的代码和tempDownloadFolder子目录中的文件?
private void moveFiles()
{
DirectoryInfo di = new DirectoryInfo(tempDownloadFolder);
FileInfo[] files = di.GetFiles();
foreach (FileInfo fi in files)
{
if (fi.Name != downloadFile)
File.Copy(tempDownloadFolder + fi.Name, destinationFolder + fi.Name, true);
}
}
答案 0 :(得分:2)
您需要进行递归搜索。
非常粗略的例子:
private void copyFiles(string filePath)
{
DirectoryInfo di = new DirectoryInfo(filePath);
FileInfo[] files = di.GetFiles();
foreach (FileInfo fi in files)
{
// test if fi is a directory
// if so call copyFiles(fi.FullName) again
// else execute the following
if (fi.Name != downloadFile) File.Copy(filePath+ fi.Name, destinationFolder + fi.Name, true);
}
}
答案 1 :(得分:1)
如果您想要所有子目录的文件使用SearchOption
参数:
DirectoryInfo di = new DirectoryInfo(tempDownloadFolder);
di.GetFiles("*.*", SearchOption.AllDirectories);
FileInfo[] files = di.GetFiles();
foreach (FileInfo fi in files)
{
if (fi.Name != downloadFile)
File.Copy(tempDownloadFolder + fi.Name, destinationFolder + fi.Name, true);
}
答案 2 :(得分:0)
用
替换File.Copy行File.Copy(fi.FullName, Path.Combine(destinationFolder, fi.Name), true);
答案 3 :(得分:0)