使用System.IO在C#中复制文件夹 [英] Copy Folders in C# using System.IO

查看:933
本文介绍了使用System.IO在C#中复制文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要复制的文件夹C:\ FromFolder到C:\ ToFolder

I need to Copy folder C:\FromFolder to C:\ToFolder

下面是code,将砍掉我的FromFolder然后将创造我的ToFolder。 所以我FromFolder将会消失,所有项目将在名为ToFolder新创建的文件夹

Below is code that will CUT my FromFolder and then will create my ToFolder. So my FromFolder will be gone and all the items will be in the newly created folder called ToFolder

System.IO.Directory.Move(@"C:\FromFolder ", @"C:\ToFolder");

但我只是想在FromFolder的文件复制到ToFolder。 出于某种原因,没有System.IO.Directory.Copy ???

But i just want to Copy the files in FromFolder to ToFolder. For some reason there is no System.IO.Directory.Copy???

这是怎么使用批处理文件来完成 - 非常容易

How this is done using a batch file - Very easy

XCOPY C:\ FromFolder C:\ ToFolder

xcopy C:\FromFolder C:\ToFolder

问候 艾蒂安

推荐答案

该链接提供了一个很好的例子。

This link provides a nice example.

<一个href="http://msdn.microsoft.com/en-us/library/cc148994.aspx">http://msdn.microsoft.com/en-us/library/cc148994.aspx

下面是一个片段

// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
//       in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
  string[] files = System.IO.Directory.GetFiles(sourcePath);

  // Copy the files and overwrite destination files if they already exist.
  foreach (string s in files)
  {
    // Use static Path methods to extract only the file name from the path.
    fileName = System.IO.Path.GetFileName(s);
    destFile = System.IO.Path.Combine(targetPath, fileName);
    System.IO.File.Copy(s, destFile, true);
  }
}

这篇关于使用System.IO在C#中复制文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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