复制所有文件目录 [英] Copy all files in directory

查看:119
本文介绍了复制所有文件目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能复制所有的内容在一个目录到另一个循环出每一个文件?

How can I copy all of the contents in one directory to another with out looping over each file?

推荐答案

您不能。无论 目录 也不<一href=\"http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx\"><$c$c>DirectoryInfo提供了一个复制方法。你需要这个工具你自己。

You can't. Neither Directory nor DirectoryInfo provide a Copy method. You need to implement this yourself.

void Copy(string sourceDir, string targetDir)
{
    Directory.CreateDirectory(targetDir);

    foreach(var file in Directory.GetFiles(sourceDir))
        File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));

    foreach(var directory in Directory.GetDirectories(sourceDir))
        Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
}

请阅读注释需要注意的一些问题,这个简单的方法。

Please read the comments to be aware of some problems with this simplistic approach.

这篇关于复制所有文件目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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