使用c#复制文件夹和所有子文件夹和文件的最佳方法是什么?

Anu*_*uya 7 c#

我需要将文件夹从一个驱动器复制到可移动硬盘.需要复制的文件夹中将包含许多子文件夹和文件.输入将是源路径和目标路径.

喜欢..

源路径:"C:\ SourceFolder"

目标路径:"E:\"

复制完成后,我可以在我的E:驱动器中看到文件夹"SourceFolder".

谢谢.

Luk*_*kas 11

我觉得这就是.

public static void CopyFolder(DirectoryInfo source, DirectoryInfo target) {
    foreach (DirectoryInfo dir in source.GetDirectories())
        CopyFolder(dir, target.CreateSubdirectory(dir.Name));
    foreach (FileInfo file in source.GetFiles())
        file.CopyTo(Path.Combine(target.FullName, file.Name));
}
Run Code Online (Sandbox Code Playgroud)


egr*_*nin 8

Channel9找到了这个.我自己没试过.

public static class DirectoryInfoExtensions
{
    // Copies all files from one directory to another.
    public static void CopyTo(this DirectoryInfo source, 
            string destDirectory, bool recursive)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (destDirectory == null)
            throw new ArgumentNullException("destDirectory");
        // If the source doesn't exist, we have to throw an exception.
        if (!source.Exists)
            throw new DirectoryNotFoundException(
                    "Source directory not found: " + source.FullName);
        // Compile the target.
        DirectoryInfo target = new DirectoryInfo(destDirectory);
        // If the target doesn't exist, we create it.
        if (!target.Exists)
            target.Create();
        // Get all files and copy them over.
        foreach (FileInfo file in source.GetFiles())
        {
            file.CopyTo(Path.Combine(target.FullName, file.Name), true);
        }
        // Return if no recursive call is required.
        if (!recursive)
            return;
        // Do the same for all sub directories.
        foreach (DirectoryInfo directory in source.GetDirectories())
        {
            CopyTo(directory, 
                Path.Combine(target.FullName, directory.Name), recursive);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法如下:

var source = new DirectoryInfo(@"C:\users\chris\desktop");
source.CopyTo(@"C:\users\chris\desktop_backup", true);
Run Code Online (Sandbox Code Playgroud)


Anu*_*uya 2

 private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
        {
            bool ret = true;
            try
            {
                SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
                DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

                if (Directory.Exists(SourcePath))
                {
                    if (Directory.Exists(DestinationPath) == false)
                        Directory.CreateDirectory(DestinationPath);

                    foreach (string fls in Directory.GetFiles(SourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
                    }
                    foreach (string drs in Directory.GetDirectories(SourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
                            ret = false;
                    }
                    Directory.CreateDirectory(DI_Target + "//Database");
                }
                else
                {
                    ret = false;
                }
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }
Run Code Online (Sandbox Code Playgroud)