递归复制文件

本文关键字:文件 复制 递归 | 更新日期: 2023-09-27 18:05:42

我找到了一个在c#中进行递归文件复制的小代码片段,但有点难倒了。我基本上需要复制一个目录结构到另一个位置,沿着下面的行…

来源:C: ' '服务器数据' mc

目标:E: ' mc

现在我的复制函数的代码是…

    //Now Create all of the directories
    foreach (string dirPath in Directory.GetDirectories(baseDir, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(baseDir, targetDir));
    }

    // Copy each file into it’s new directory.
    foreach (string file in Directory.GetFiles(baseDir, "*.*", SearchOption.AllDirectories))
    {
        Console.WriteLine(@"Copying {0}'{1}", targetDir, Path.GetFileName(file));
        if (!CopyFile(file, Path.Combine(targetDir, Path.GetFileName(file)), false))
        {
            int err = Marshal.GetLastWin32Error();
            Console.WriteLine("[ERROR] CopyFile Failed on {0} with code {1}", file, err);
        }
    }
问题是,在第二个作用域中,我要么:
  1. 使用Path.GetFileName(file)来获得没有路径的实际文件名,但我丢失了目录"mc"目录结构
  2. 使用"file"而不使用Path.Combine.

无论哪种方式,我都必须做一些讨厌的字符串工作。在c#中是否有一个好的方法来做到这一点(我缺乏对。net API的了解导致我过于复杂的事情)

递归复制文件

MSDN有完整的示例:如何:复制目录

using System;
using System.IO;
class DirectoryCopyExample
{
    static void Main()
    {
        // Copy from the current directory, include subdirectories.
        DirectoryCopy(".", @".'temp", true);
    }
    private static void DirectoryCopy(string sourceDirName, string destDirName, 
                                      bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }
        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }
        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
        }
        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}

这个答案的非递归替换将是:

private static void DirectoryCopy(string sourceBasePath, string destinationBasePath, bool recursive = true)
{
    if (!Directory.Exists(sourceBasePath))
        throw new DirectoryNotFoundException($"Directory '{sourceBasePath}' not found");
    var directoriesToProcess = new Queue<(string sourcePath, string destinationPath)>();
    directoriesToProcess.Enqueue((sourcePath: sourceBasePath, destinationPath: destinationBasePath));
    while (directoriesToProcess.Any())
    {
        (string sourcePath, string destinationPath) = directoriesToProcess.Dequeue();
        if (!Directory.Exists(destinationPath))
            Directory.CreateDirectory(destinationPath);
        var sourceDirectoryInfo = new DirectoryInfo(sourcePath);
        foreach (FileInfo sourceFileInfo in sourceDirectoryInfo.EnumerateFiles())
            sourceFileInfo.CopyTo(Path.Combine(destinationPath, sourceFileInfo.Name), true);
        if (!recursive)
            continue;
        foreach (DirectoryInfo sourceSubDirectoryInfo in sourceDirectoryInfo.EnumerateDirectories())
            directoriesToProcess.Enqueue((
                sourcePath: sourceSubDirectoryInfo.FullName,
                destinationPath: Path.Combine(destinationPath, sourceSubDirectoryInfo.Name)));
    }
}

代替

foreach (string file in Directory.GetFiles(baseDir, "*.*", SearchOption.AllDirectories))
{

做这样的事情

foreach (FileInfo fi in source.GetFiles())
{
     fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}