如何在。net中复制文件夹和所有子文件夹和文件?(复制)

时间:2022-08-16 07:05:19

Possible Duplicate:
Best way to copy the entire contents of a directory in C#

可能的重复:在c#中复制目录的全部内容的最佳方式

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?

我想把所有子文件夹和文件从一个位置复制到。net的另一个位置。最好的方法是什么?

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.

我在System.IO上看到了复制方法。文件类,但是想知道是否有比抓取目录树更简单、更好或更快的方法。

3 个解决方案

#1


49  

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

有VisualBasic。Steve引用的dll实现,这是我用过的。

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

#2


12  

Michal Talaga references the following in his post:

Michal Talaga在他的文章中提到:

  • Microsoft's explanation about why there shouldn't be a Directory.Copy() operation in .NET.
  • 微软解释为什么在。net中不应该有目录。copy()操作。
  • An implementation of CopyDirectory() from the Microsoft.VisualBasic.dll assembly.
  • 来自Microsoft.VisualBasic的CopyDirectory()实现。dll组装。

However, a recursive implementation based on File.Copy() and Directory.CreateDirectory() should suffice for the most basic of needs.

但是,基于File.Copy()和Directory.CreateDirectory()的递归实现应该能够满足最基本的需求。

#3


2  

If you don't get anything better... perhaps use Process.Start to fire up robocopy.exe?

如果你没有更好的东西……也许使用过程。启动机器人程序。exe?

#1


49  

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

有VisualBasic。Steve引用的dll实现,这是我用过的。

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

#2


12  

Michal Talaga references the following in his post:

Michal Talaga在他的文章中提到:

  • Microsoft's explanation about why there shouldn't be a Directory.Copy() operation in .NET.
  • 微软解释为什么在。net中不应该有目录。copy()操作。
  • An implementation of CopyDirectory() from the Microsoft.VisualBasic.dll assembly.
  • 来自Microsoft.VisualBasic的CopyDirectory()实现。dll组装。

However, a recursive implementation based on File.Copy() and Directory.CreateDirectory() should suffice for the most basic of needs.

但是,基于File.Copy()和Directory.CreateDirectory()的递归实现应该能够满足最基本的需求。

#3


2  

If you don't get anything better... perhaps use Process.Start to fire up robocopy.exe?

如果你没有更好的东西……也许使用过程。启动机器人程序。exe?