C#:将乘法文件复制到文件夹中,并保持文件夹结构

时间:2018-08-16 09:46:03

标签: c# file copy directory

我想将“主文件夹”和子文件夹中存在的所有.txt文件复制到“主文件夹复制”中。

Main-Folder
├── Folder 1
│   ├── file0.jpg
│   ├── file1.ini
│   ├── file2.dll
│   └── file3.txt
└── Folder 2
    ├── file4.dll
    ├── file5.txt
    └── Folder 3
        ├── file6.txt
        ├── file7.jpg
        ├── file8.dll
        └── file9.txt

复制到其他位置时,我要保持这样的文件夹结构:

Main-Folder-Copy
├── Folder 1
│   └── file3.txt
└── Folder 2
    ├── file5.txt
    └── Folder 3
        ├── file6.txt
        └── file9.txt

我的想法是将整个“主文件夹”复制到一个临时文件夹中,然后删除所有不必要的文件。当面对较大的文件夹时,这可能会成为问题,因为必须首先复制整个文件夹。有更好的解决方案吗?

我尝试使用System.IO.Directory.EnumerateFiles

3 个答案:

答案 0 :(得分:1)

以下代码摘自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);
            }
        }
    }
}

答案 1 :(得分:1)

以下示例可以为您提供帮助:

string dirA = @"Main-Folder\";
string dirB = @"Main-Folder-Copy\";

string[] files = System.IO.Directory.GetFiles(dirA);

foreach (string s in files) {
    if (System.IO.Path.GetExtension(s).equals(".txt")) {
        System.IO.File.Copy(s, System.IO.Path.Combine(targetPath, fileName), true);
    }
}

答案 2 :(得分:1)

这对我有用,与以前的MSDN答案非常相似:

public static void Copy(string sourceDirectoryName, string targetDirectoryName)
{
    Directory.CreateDirectory(targetDirectoryName);

    DirectoryInfo source = new DirectoryInfo(sourceDirectoryName);
    DirectoryInfo target = new DirectoryInfo(targetDirectoryName);

    CopyWork(source, target);
}

private static void CopyWork(DirectoryInfo source, DirectoryInfo target)
{
    foreach (DirectoryInfo dir in source.GetDirectories())
        CopyWork(dir, target.CreateSubdirectory(dir.Name));

    foreach (FileInfo file in source.GetFiles())
        file.CopyTo(Path.Combine(target.FullName, file.Name));
}

由于某种原因,他们从未放过System.IO.Directory.Copy()