如何在.NET中复制文件夹及其所有子文件夹和文件? [英] How to copy a folder and all it's subfolders and files in .NET?

查看:193
本文介绍了如何在.NET中复制文件夹及其所有子文件夹和文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!朋友,我想将包含文件和子文件夹的文件夹复制到目标文件夹.帮帮我..
实际上,我正在将文件的源文件夹复制到目标位置,但是仅复制了文件,我无法在目标文件夹中复制文件的文件夹...我用了这些代码..帮助我...

Hello! Friend i want to copy a folder with it''s file and subfolder to destination folder.Help me out ..
actualy i am copying the source folder wtih it''s file to destination but only file is copied i could not copied folder wtih file in destination folder ...i used these code ..help me...

private static void CopyDirectory(string sourcePath, string destPath)
{
    if (!Directory.Exists(destPath))
    {
        Directory.CreateDirectory(destPath);
    }

    foreach (string file in Directory.GetFiles(sourcePath))
    {
        string dest = Path.Combine(destPath, Path.GetFileName(file));
        File.Copy(file, dest);
    }

    foreach (string folder in Directory.GetDirectories(sourcePath))
    {
        string dest = Path.Combine(destPath, Path.GetFileName(folder));
        CopyDirectory(folder, dest);
    }
}

推荐答案

我认为在您的第二个循环中,您想替换
I think in your second loop you want to replace
string dest = Path.Combine(destPath, Path.GetFileName(folder));




with

string dest = Path.Combine(destPath, Path.GetDirectoryName(folder));


看起来您的代码类似于此处的代码:

http://msdn.microsoft.com/en-us/library/bb762914.aspx [ ^ ]

我建议您阅读该MSDN文章,并弄清楚为什么每个部分都在做自己正在做的事情.最后,您只需要复制并粘贴代码即可使用,但是如果您真正了解它在做什么的原因,那么对您来说会更好.
It looks like your code is similar to what is here:

http://msdn.microsoft.com/en-us/library/bb762914.aspx[^]

I would recommend you go through that MSDN article and figure out why each piece is doing what it is doing. In the end, you could just copy and paste the code and it would work but it is much better for you if you actually learn why it is doing what it is doing.


您可能正在路径有问题.如果最后有一个斜杠,那么您有问题.以下似乎有效:

You probably are having problems with the path. If you have a slash at the end, then you have a problem. The following seems to work:

private static void CopyDirectory(string sourcePath, string destPath)
{

  if (!Directory.Exists(destPath))
  {
    Directory.CreateDirectory(destPath);
  }
  var fixedSource = new DirectoryInfo(sourcePath);
  var fixedDest = new DirectoryInfo(destPath);

  foreach (var file in fixedSource.GetFiles())
  {
    string dest = Path.Combine(fixedDest.FullName, file.Name);
    file.CopyTo(dest);
  }

  foreach (var folder in fixedSource.GetDirectories())
  {
    string dest = Path.Combine(destPath, folder.Name);
    CopyDirectory(folder.FullName, dest);
  }
}



这不是我建议您这样做的方式.我将有一个非递归方法来解决所有路径问题,然后您的代码可以工作,但是您可能会遇到重复问题,并且还需要处理.



This is not the way I would recommend that you do it. I would have a non-recursive method that would fix any path issues, then your code will work, but you may have an issue with duplicates, and you need to deal with that also.


这篇关于如何在.NET中复制文件夹及其所有子文件夹和文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆