C# 复制遍历文件夹下所有文件夹内指定文件 ,到指定位置

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserWAVE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_原文件_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            //dialog.RootFolder = Environment.SpecialFolder.Programs;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
              textBox1.Text = dialog.SelectedPath;
            }
        }
        private void btn_目标文件_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            //dialog.RootFolder = Environment.SpecialFolder.Programs;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = dialog.SelectedPath;
            }
        }
        private void btn_OK_Click(object sender, EventArgs e)
        {
            GetDirectory(textBox1.Text);
           var a= directorysList;
            // CopyFolder2(textBox1.Text, textBox1.Text);
            for (int i = 0; i < directorysList.Count; i++)
            {
                CopyOldLabFilesToNewLab(directorysList[i].ToString(), textBox2.Text);
            }
        }
       
        /// 
        /// 拷贝oldlab的文件到newlab下面
        /// 
        /// lab文件所在目录(@"~\labs\oldlab")
        /// 保存的目标目录(@"~\labs\newlab")
        /// 返回:true-拷贝成功;false:拷贝失败
        public bool CopyOldLabFilesToNewLab(string sourcePath, string savePath)
        {
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            #region //拷贝labs文件夹到savePath下
            try
            {

                string[] labDirs = Directory.GetDirectories(sourcePath);//目录
                string[] labFiles = Directory.GetFiles(sourcePath);//文件
                if (labFiles.Length > 0)
                {
                    for (int i = 0; i < labFiles.Length; i++)
                    {
                        if (Path.GetExtension(labFiles[i]) == ".wave")//排除.lab文件
                        {
                            File.Copy(sourcePath + "\\" + Path.GetFileName(labFiles[i]), savePath + "\\" + Path.GetFileName(labFiles[i]), true);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            #endregion
            return true;
        }
        /// 
        /// 复制文件夹及文件
        /// 
        /// 原文件路径
        /// 目标文件路径
        /// 
        public int CopyFolder2(string sourceFolder, string destFolder)
        {
            try
            {
                string folderName = System.IO.Path.GetFileName(sourceFolder);
                string destfolderdir = System.IO.Path.Combine(destFolder, folderName);
                string[] filenames = System.IO.Directory.GetFileSystemEntries(sourceFolder);
                foreach (string file in filenames)// 遍历所有的文件和目录
                {
                    if (System.IO.Directory.Exists(file))
                    {
                        string currentdir = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file));
                        if (!System.IO.Directory.Exists(currentdir))
                        {
                            System.IO.Directory.CreateDirectory(currentdir);
                        }
                        //string[] labDirs = Directory.GetDirectories(sourcePath);//目录
                        //string[] labFiles = Directory.GetFiles(sourcePath);//文件
                        //if (labFiles.Length > 0)
                        //{
                        //    for (int i = 0; i < labFiles.Length; i++)
                        //    {
                        //        if (Path.GetExtension(labFiles[i]) == ".wave")//排除.lab文件
                        //        {
                        //            File.Copy(sourcePath + "\\" + Path.GetFileName(labFiles[i]), savePath + "\\" + Path.GetFileName(labFiles[i]), true);
                        //        }
                        //    }
                        //}

                        CopyFolder2(file, destfolderdir);
                    }
                    //else3
                    //{
                    //    string srcfileName = System.IO.Path.Combine(destfolderdir, System.IO.Path.GetFileName(file));
                    //    if (!System.IO.Directory.Exists(destfolderdir))
                    //    {
                    //        System.IO.Directory.CreateDirectory(destfolderdir);
                    //    }
                    //    System.IO.File.Copy(file, srcfileName);
                    //}
                }

                return 1;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return 0;
            }

        }
        
            /*利用静态数据来存储文件路径列表*/
            private   ArrayList directorysList = new ArrayList();//存储目录列表数据

            public   ArrayList DirectorysList
            {
                get { return directorysList; }
                set { directorysList = value; }
            }
            private   ArrayList fileList = new ArrayList();//存储文件路径列表

            public   ArrayList FileList
            {
                get { return fileList; }
                set { fileList = value; }
            }
            public   void GetDirectory(string sourcePath)
            {
                if (Directory.Exists(sourcePath))//判断源文件夹是否存在
                {
                    string[] tmp = Directory.GetFileSystemEntries(sourcePath);//获取源文件夹中的目录及文件路径,存入字符串
                                                                              //循环遍历
                    for (int i = 0; i < tmp.Length; i++)
                    {
                        if (File.Exists(tmp[i]))//如果是文件则存入FileList
                        {
                            FileList.Add(tmp[i]);
                        }
                        else
                        {
                            if ((Directory.GetDirectories(tmp[i])).Length == 0)//如果是最后一层目录则把其路径存入DirectorysList
                            {
                                DirectorysList.Add(tmp[i]);
                            }
                        }
                        //递归开始.......
                        GetDirectory(tmp[i]);
                    }
                }
            }
    }
}

有些代码用不上  个人需求修改吧!

下载链接:https://download.csdn.net/download/qq_36074218/36726847

你可能感兴趣的:(winform,c#,.net,c#,开发语言,后端)