遍历文件目录下所有图片并保存到统一路径

遍历文件夹下所有文件(包括文件夹),找到所有图片并将其统一保存至目标文件夹下:

            DirectoryInfo theFolder = new DirectoryInfo(@"I:\ph\"); //目标文件夹
            DirectoryInfo[] dirInfo = theFolder.GetDirectories();
            //遍历文件夹
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                // this.listBox1.Items.Add(NextFolder.Name);
                FileInfo[] fileInfo = NextFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)
                {//遍历文件
                    this.listBox1.Items.Add(NextFile.Name);
                    Random ran = new Random();//产生随机数,避免出现重名的图片
                    int RandKey = ran.Next(100, 999);
                    NextFile.CopyTo(@"I:\pictures\" + (RandKey + NextFile.Name));//复制图片至该路径
                }
            }

 

posted @ 2016-05-21 21:25  贝尔格里尔斯  阅读(1116)  评论(0编辑  收藏  举报