开发者

How do you copy all content of one folder( copy files and folders)

开发者 https://www.devze.com 2023-03-05 20:48 出处:网络
I want to copy all the files, and copy all the folders, included empty folder, and开发者_StackOverflow files protected, files not visibles, folder or files any way! copy all! The following sample take

I want to copy all the files, and copy all the folders, included empty folder, and开发者_StackOverflow files protected, files not visibles, folder or files any way! copy all!


The following sample taken from MSDN's documentation of the DirectoryInfo class shows how to copy a directory:

using System;
using System.IO;

class CopyDir
{
    public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        if (source.FullName.ToLower() == target.FullName.ToLower())
        {
            return;
        }

        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        // Copy each file into it's new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }

    public static void Main()
    {
        string sourceDirectory = @"c:\sourceDirectory";
        string targetDirectory = @"c:\targetDirectory";

        DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
        DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

        CopyAll(diSource, diTarget);
    }

    // Output will vary based on the contents of the source directory.
}


http://www.csharp411.com/c-copy-folder-recursively/
http://www.logiclabz.com/c/copy-directory-in-net-c-including-sub-folders.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消