当前位置: 首页 > news >正文

文件夹复制操作(非递归循环遍历文件夹)

/// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="SourcePath">原始路径</param>
        /// <returns></returns>
        public static bool CreateFolder(string SourcePath)
        {
            try
            {
                Directory.CreateDirectory(SourcePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

 /// <summary>
        /// 复制文件夹[循环遍历]
        /// </summary>
        /// <param name="SourcePath">原始路径</param>
        /// <param name="DestinPath">目地的路径</param>
        /// <returns></returns>
        public static bool  CopyFolder(string SourcePath, string DestinPath)
        {
            if (Directory.Exists(SourcePath))
            {
                CreateFolder(DestinPath);//第一次创建跟目录文件夹
                string sourcePath = SourcePath;//[变化的]原始路径
                string destinPath = DestinPath;//[变化的]目地的路径
                Queue<string> source = new Queue<string>();//存原始文件夹路径
                Queue<string> destin = new Queue<string>();//存目地的文件夹路径
                bool IsHasChildFolder = true;//是否有子文件夹
                string tempDestinPath = string.Empty;//临时目地的,将被存于destin中
                while (IsHasChildFolder)
                {
                    string[] fileList = Directory.GetFileSystemEntries(sourcePath);// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                    for (int i = 0; i < fileList.Length; i++)// 遍历所有的文件和目录
                    {
                        tempDestinPath = destinPath + "\\" + Path.GetFileName(fileList[i]);//取得子文件路径
                        if (Directory.Exists(fileList[i]))//存在文件夹时
                        {
                            source.Enqueue(fileList[i]);//当前的子目录的原始路径进队列
                            destin.Enqueue(tempDestinPath);//当前的子目录的目地的路径进队列
                            CreateFolder(tempDestinPath);//创建子文件夹
                        }
                        else//存在文件
                        {
                            File.Copy(fileList[i], tempDestinPath, true);//复制文件
                        }
                    }
                    if (source.Count > 0 && source.Count == destin.Count)//存在子文件夹时
                    {
                        sourcePath = source.Dequeue();
                        destinPath = destin.Dequeue();
                    }
                    else
                    {
                        IsHasChildFolder = false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:http://www.cnblogs.com/cyq1162/archive/2007/05/28/762294.html

相关文章:

  • 无人机如何帮助黑客关闭电厂或其他基础设施
  • 河北实施工业云与大数据试点示范工程
  • 阿里云设举报中心:拒绝一切网络灰黑产
  • CRM产品设计思考
  • 危险:这五种情况不宜洗澡
  • 纵横客CRM助力9天销售额暴增360%
  • 眼睛变形、度数增加,全是眼镜惹的祸?关于眼镜的 5 大误区
  • 设计模式入门,工厂模式,c++代码实现
  • 线缆企业+缆360成标配 天津线缆展看产业发展未来
  • 希捷硬盘扩容软件-----DiscWizard
  • 拥抱开源!解析IBM Power8与Hadoop的不解之缘
  • 家庭布线需谨慎,老司机带你避开十大雷区
  • 报告称Flash视频格式2年内彻底消失
  • 阿里云核心产品再次降价:最高降幅35%
  • 网络运营者是个人信息保护的第一责任人
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • Bootstrap JS插件Alert源码分析
  • CentOS 7 修改主机名
  • gitlab-ci配置详解(一)
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • JS字符串转数字方法总结
  • Laravel5.4 Queues队列学习
  • Mysql5.6主从复制
  • SegmentFault 2015 Top Rank
  • SpriteKit 技巧之添加背景图片
  • webgl (原生)基础入门指南【一】
  • 多线程 start 和 run 方法到底有什么区别?
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 计算机常识 - 收藏集 - 掘金
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 线上 python http server profile 实践
  • 小李飞刀:SQL题目刷起来!
  • 一个JAVA程序员成长之路分享
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​油烟净化器电源安全,保障健康餐饮生活
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • (1)(1.11) SiK Radio v2(一)
  • (a /b)*c的值
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (C)一些题4
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (三)uboot源码分析
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .NET Core 通过 Ef Core 操作 Mysql
  • .py文件应该怎样打开?
  • @Transient注解
  • [2008][note]腔内级联拉曼发射的,二极管泵浦多频调Q laser——
  • [20170713] 无法访问SQL Server
  • [20180129]bash显示path环境变量.txt
  • [android学习笔记]学习jni编程
  • [Ariticle] 厚黑之道 一 小狐狸听故事
  • [C#][opencvsharp]opencvsharp sift和surf特征点匹配