复制目录和文件时出现问题

时间:2019-02-24 07:24:12

标签: c#

我想使用C#复制特定目录以及所有子目录和文件。

使用此代码,我遇到了问题。这段代码在子文件夹中创建了另一个同名文件夹,但是我找不到错误所在。

目录路径=“> D:\FolderAAA\FolderBBB\File1
目标(结果):=> E:\FolderAAA\FolderBBB\FolderBBB\File1(带有双bbb文件夹的问题)

这是我的代码

private void CopyTheDirectory(string directoryPath, string targetPath)
{
    DirectoryInfo d_info = new DirectoryInfo(directoryPath);
    Directory.CreateDirectory(Path.Combine(targetPath, d_info.Name));

    var files = Directory.GetFiles(d_info.FullName);
    var directories = Directory.GetDirectories(directoryPath);

    foreach(var file in files)
    {
        File.Copy(file, Path.Combine(Path.Combine(targetPath, d_info.Name), Path.GetFileName(file)));
    }

    foreach(var directory in directories)
    {

        CopyTheDirectory(directory, Path.Combine(Path.Combine(targetPath, d_info.Name), Path.GetFileName(directory)));
    }
}

2 个答案:

答案 0 :(得分:1)

您弄错了子目录。您应该使用d_info.GetDirectories,它将代替返回DirectoryInfo的数组。 这使得文件和子目录的目标路径的构建变得更加简单,并且不易出错。

这是您代码的固定版本:

private void CopyTheDirectory(string directoryPath, string targetPath)
{
    DirectoryInfo d_info = new DirectoryInfo(directoryPath);
    Directory.CreateDirectory(targetPath);

    var files = Directory.GetFiles(d_info.FullName);
    var directories = d_info.GetDirectories();

    foreach (var file in files)
    {
        File.Copy(file, Path.Combine(targetPath, Path.GetFileName(file)));
    }

    foreach (var directory in directories)
    {
        CopyTheDirectory(directory.FullName, Path.Combine(targetPath, directory.Name));
    }
}

更新

根据您对答案的评论,以下是我在寻找的内容:

private void CopyTheDirectory(string directoryPath, string targetPath)
{
    DirectoryInfo d_info = new DirectoryInfo(directoryPath);

    var toPath = targetPath.EndsWith(Path.DirectorySeparatorChar.ToString() + d_info.Name) ? targetPath : Path.Combine(targetPath, d_info.Name);
    Directory.CreateDirectory(toPath);

    var files = Directory.GetFiles(d_info.FullName);
    var directories = d_info.GetDirectories();

    foreach (var file in files)
    {
        File.Copy(file, Path.Combine(toPath, Path.GetFileName(file)));
    }

    foreach (var directory in directories)
    {
        CopyTheDirectory(directory.FullName, Path.Combine(toPath, directory.Name));
    }
}

答案 1 :(得分:0)

这必须工作!

public void CopyFolderNdContents(string sourceFolder, string destFolder)
    {
        try
        {
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);

            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);

                if (!File.Exists(file))  // If file exists don't copy !!
                    File.Copy(file, dest);
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                CopyFolderNdContents(folder, dest);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

private void btnCopyFoder_Click(object sender, EventArgs e)
    {
        CopyFolderNdContents(@"C:\FolderAAA", @"D:\FolderAAA");
    }