什么是一个文件夹,使用C#的所有子文件夹和文件复制的最佳方式 [英] What is the best way to copy a folder and all subfolders and files using c#

查看:222
本文介绍了什么是一个文件夹,使用C#的所有子文件夹和文件复制的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个文件夹从一个驱动器复制到移动硬盘。
需要被复制就会有很多的子文件夹和文件的文件夹。
的投入将是源路径和目标路径



像..



源路径: C:\SourceFolder



目标路径:E:\



在复制完成我shud能看到我的电子文件夹SourceFolder:驱动器



感谢


解决方案

 私有静态布尔CopyDirectory(SOURCEPATH字符串,字符串的DestinationPath,布尔overwriteexisting)
{
布尔RET = TRUE;

{
SOURCEPATH = SourcePath.EndsWith(@\)? SOURCEPATH:SOURCEPATH + @\;
=的DestinationPath DestinationPath.EndsWith(@\)?的DestinationPath:+的DestinationPath @\;

如果(Directory.Exists(SOURCEPATH))
{
如果(Directory.Exists(的DestinationPath)==假)
Directory.CreateDirectory(的DestinationPath);

的foreach(在Directory.GetFiles字符串FLS(SOURCEPATH))
{
FileInfo的flinfo =新的FileInfo(FLS);
flinfo.CopyTo(+的DestinationPath flinfo.Name,overwriteexisting);
}
的foreach(在Directory.GetDirectories字符串DRS(SOURCEPATH))
{
DirectoryInfo的drinfo =新DirectoryInfo的(DRS);
如果(CopyDirectory(DRS,+的DestinationPath drinfo.Name,overwriteexisting)==假)
RET = FALSE;
}
Directory.CreateDirectory(DI_Target +//数据库);
}
,否则
{
RET = FALSE;
}
}
赶上(异常前)
{
RET = FALSE;
}
返回RET;
}


I need to copy a Folder from one drive to a removable Hard disk. The Folder which needs to be copied will have many sub folders and files in it. The input will be Source Path and Target Path.

Like..

Source Path : "C:\SourceFolder"

Target Path : "E:\"

After copying is done, i shud be able to see the folder "SourceFolder" in my E: drive.

Thanks.

解决方案

 private static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
        {
            bool ret = true;
            try
            {
                SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
                DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

                if (Directory.Exists(SourcePath))
                {
                    if (Directory.Exists(DestinationPath) == false)
                        Directory.CreateDirectory(DestinationPath);

                    foreach (string fls in Directory.GetFiles(SourcePath))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
                    }
                    foreach (string drs in Directory.GetDirectories(SourcePath))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
                            ret = false;
                    }
                    Directory.CreateDirectory(DI_Target + "//Database");
                }
                else
                {
                    ret = false;
                }
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }

这篇关于什么是一个文件夹,使用C#的所有子文件夹和文件复制的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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