惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
Cloudbric
Cloudbric
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
博客园_首页
The Cloudflare Blog
C
Cisco Blogs
AWS News Blog
AWS News Blog
IT之家
IT之家
Cyberwarzone
Cyberwarzone
罗磊的独立博客
美团技术团队
V
V2EX
Project Zero
Project Zero
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
S
Schneier on Security
P
Privacy International News Feed
V
Visual Studio Blog
量子位
T
Tor Project blog
S
Securelist
腾讯CDC
A
About on SuperTechFans
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
B
Blog RSS Feed
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
B
Blog
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
V
Vulnerabilities – Threatpost
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
L
LINUX DO - 热门话题
Recorded Future
Recorded Future

博客园 - zhengfeng

GridView 72般绝技 链接打开固定大小的新窗口 iframe传值和自适应高度 页面传参 小技巧 js表单验证控制代码大全 - zhengfeng - 博客园 递归写的复制文件夹及其下的内容(原样复制) JS取得URL中的参数值 自己写的将文件从多个文件合并到一个文件夹的小方法 使用 Hashtable 集合(一) MessageBox.Show用法 System.IO.File类和System.IO.FileInfo类 Math.floor和Math.ceil函数 Document对象 javascript小技巧 JavaScript的学习 正则表达式的学习: 工作小结: DataList分页
自己写的递归方法复制文件夹里面的内容(从源文件(里面可以有多个层次的子文件夹)到一个文件夹)
zhengfeng · 2007-07-23 · via 博客园 - zhengfeng

 private void CopyFile(string source, string destination)
        {
                bool flag = true;
                if (!Directory.Exists(destination) && flag == true)
                {
                    Directory.CreateDirectory(destination);
                    flag = false;
                }
                DirectoryInfo rootdir = new DirectoryInfo(source);

                //遍历文件  
                FileInfo[] fileinfo = rootdir.GetFiles();
                foreach (FileInfo file in fileinfo)
                {
                    file.CopyTo(destination + "\\" + file.Name, true);
                }
                DirectoryInfo[] childdir = rootdir.GetDirectories();
                for (int i = 0; i < childdir.Length; i++)
                {
                    try
                    {
                        FileInfo[] fileinfo_child = childdir[i].GetFiles();
                        foreach (FileInfo file_child in fileinfo_child)
                        {
                            file_child.CopyTo(destination + "\\" + file_child.Name, true);
                        }
                        //递归方法
                        DirectoryInfo[] childdir_child = childdir[i].GetDirectories();
                        if (childdir_child.Length > 0)
                        {
                            for (int j = 0; j < childdir_child.Length; j++)
                            {
                                CopyFile(source + "\\" + childdir[i].Name + "\\" + childdir_child[j].Name, destination);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.Message);
                        continue;
                    }
                }
          } 
--------------------------------------------------------------
另外一种方法:
  // 复制合并图片到一个文件夹
        static void CopyPicToFileDirectory()
        {
            Console.WriteLine("请输入源文件夹路径:");
            string sPath = Console.ReadLine();
            if (!Directory.Exists(sPath))
            {
                Console.WriteLine("源文件夹路径不存在!");
                return;
            }

            Console.WriteLine("请输入目标文件夹路径:");
            string dPath = Console.ReadLine();
            if (!Directory.Exists(dPath))
            {
                Console.WriteLine("目标文件夹路径不存在!");
                return;
            }
           
           
            Console.WriteLine("是否删除原文件:Press [Y]/[N]");
            bool isMove = false;
            string str1 = null;
            str1 = Console.ReadLine();
            bool flag = (str1 == null)?false:true;
            while (flag)
            {
                if ("y".Equals(str1.Trim().ToLower()))
                {
                    //yes
                    isMove = true;
                    break;
                }
                else if ("n".Equals(str1.Trim().ToLower()))
                {
                    //no
                    isMove = false;
                    break;
                }
                Console.WriteLine("是否删除原文件:Press [Y]/[N]");
                str1 = Console.ReadLine();
                flag = (str1 == null) ? false : true;
            }

            DoCopyFile(sPath, dPath, isMove);
        }

        private static void DoCopyFile(string sPath, string dPath, bool isMove)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(sPath);
            string destionFilename = null;
            FileInfo[] fileInfos = directoryInfo.GetFiles();
            foreach (FileInfo f in fileInfos)
            {
                destionFilename = dPath.Trim('\\') + "\\" + f.Name;
                f.CopyTo(destionFilename, true);
                if (isMove)
                {
                    f.Delete();
                }
            }
            DirectoryInfo[] subDirectories = directoryInfo.GetDirectories();
            foreach (DirectoryInfo d in subDirectories)
            {
                DoCopyFile(d.FullName, dPath, isMove);
            }
        }