使用System.IO在C#中复制文件夹

编程入门 行业动态 更新时间:2024-10-25 15:34:43
本文介绍了使用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.

msdn.microsoft/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#中复制文件夹

本文发布于:2023-11-09 22:52:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   System   IO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!