什么是递归复制在C#中的内容的最佳方式? [英] What is the best way to recursively copy contents in C#?

查看:129
本文介绍了什么是递归复制在C#中的内容的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用C#和ASP.NET?

What is the best way to recursively copy a folder's content into another folder using C# and ASP.NET?

推荐答案

那么你可以试试这个

DirectoryInfo sourcedinfo = new DirectoryInfo(@"E:\source");
DirectoryInfo destinfo = new DirectoryInfo(@"E:\destination");
copy.CopyAll(sourcedinfo, destinfo);

这是做好一切工作的方法:

and this is the method that do all the work:

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
    try
    {
        //check if the target directory exists
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        //copy all the files into the new directory

        foreach (FileInfo fi in source.GetFiles())
        {
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }


        //copy all the sub directories using recursion

        foreach (DirectoryInfo diSourceDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
            CopyAll(diSourceDir, nextTargetDir);
        }
        //success here
    }
    catch (IOException ie)
    {
        //handle it here
    }
}

我希望这将有助于:)

I hope this will help :)

这篇关于什么是递归复制在C#中的内容的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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