首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用c#复制文件夹及其所有子文件夹和文件的最佳方式是什么

使用c#复制文件夹及其所有子文件夹和文件的最佳方式是什么
EN

Stack Overflow用户
提问于 2010-04-30 12:29:53
回答 7查看 19.6K关注 0票数 8

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

就像..。

源路径:"C:\SourceFolder“

目标路径:"E:\“

复制完成后,我应该能够在E:驱动器中看到文件夹"SourceFolder“。

谢谢。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-05-18 17:40:29

代码语言:javascript
复制
 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;
        }
票数 2
EN

Stack Overflow用户

发布于 2010-04-30 12:52:52

我想就是这里了。

代码语言:javascript
复制
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));
}
票数 12
EN

Stack Overflow用户

发布于 2010-04-30 12:36:22

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

代码语言:javascript
复制
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);
        }
    }
}

其用法如下所示:

代码语言:javascript
复制
var source = new DirectoryInfo(@"C:\users\chris\desktop");
source.CopyTo(@"C:\users\chris\desktop_backup", true);
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2742300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档