0

如何修改 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);
   }

}
4

4 回答 4

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

    }
于 2013-05-23T11:50:21.790 回答
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);
}
于 2013-05-23T11:48:43.713 回答
0

只需在这里找到有效的解决方案:

http://www.codeproject.com/Articles/3210/Function-to-copy-a-directory-to-another-place-noth?msg=4571843#xx4571843xx

于 2013-05-23T23:16:46.537 回答
0

将 File.Copy 行替换为

File.Copy(fi.FullName, Path.Combine(destinationFolder, fi.Name), true);
于 2013-05-23T11:52:58.470 回答