如何复制一个文件夹,在.NET中所有子文件夹和文件? [英] How do I copy a folder and all subfolders and files in .NET?

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

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp">Best方式的目录的全部内容复制在C#的

我想从一个位置复制文件夹及其所有子文件夹和文件到另一个.NET。什么是做到这一点的最好方法是什么?

I'd like to copy folder with all its subfolders and file from one location to another in .NET. What's the best way to do this?

我看到有System.IO.File类的复制方法,但不知道是否有比抓取的目录树更容易,更好,或者更快的方式。

I see the Copy method on the System.IO.File class, but was wondering whether there was an easier, better, or faster way than to crawl the directory tree.

推荐答案

嗯,还有的VisualBasic.dll实现,史蒂夫引用,这里的东西,我用。

Well, there's the VisualBasic.dll implementation that Steve references, and here's something that I've used.

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);
    }
}

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

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