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

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 【当耐特】
月光博客
月光博客
Vercel News
Vercel News
D
Docker
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
有赞技术团队
有赞技术团队
雷峰网
雷峰网
博客园 - 聂微东
小众软件
小众软件
Y
Y Combinator Blog
腾讯CDC
L
LangChain Blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss

博客园 - 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);
            }
        }