基本信息
源码名称:根据记事本中的文件夹名,复制指定文件夹下的对应文件夹到指定位置
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2020-06-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            folder.Description = "选择所有文件存放目录";
            if (folder.ShowDialog() == DialogResult.OK)
            {

                string sPath = folder.SelectedPath;
                //MessageBox.Show(sPath);
                txt_y.Text = sPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            folder.Description = "选择所有文件存放目录";
            if (folder.ShowDialog() == DialogResult.OK)
            {

                string sPath = folder.SelectedPath;
                //MessageBox.Show(sPath);
                txt_m.Text = sPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
            ofd.ValidateNames = true;
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string strFileName = ofd.FileName;
                txt_w.Text = strFileName;              
                
              

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (txt_y.Text!= null && txt_m.Text != null && txt_w.Text != null)
            {
                string sStuName = string.Empty;
                FileStream fs = new FileStream(txt_w.Text, FileMode.Open);
                //// "GB2312"用于显示中文字符,写其他的,中文会显示乱码
                StreamReader reader = new StreamReader(fs, UnicodeEncoding.GetEncoding("GB2312"));
                progressBar1.Visible=true;
                label4.Text = "运算中...";
                label4.Visible = true;
                progressBar1.Value = 0;
                progressBar1.Maximum = 100;
                progressBar1.Step = 1;//设置没次增长多少

                //// 一行一行读取
                while ((sStuName = reader.ReadLine()) != null)
                {
                    sStuName = sStuName.Trim().ToString();

                    if (Directory.Exists(txt_y.Text   "\\"   sStuName))
                    {
                        CopyOldLabFilesToNewLab(txt_y.Text   "\\"   sStuName, txt_m.Text   "\\"   sStuName);

                    }
                    else
                    {

                        if (!System.IO.File.Exists(txt_m.Text   "/"    "Log"   DateTime.Today.ToString("yyyy-MM-dd")   ".txt"))
                        {
                            FileStream fs1 = new FileStream(txt_m.Text   "/"    "Log"   DateTime.Today.ToString("yyyy-MM-dd")   ".txt", FileMode.Create, FileAccess.Write);//创建写入文件 
                            StreamWriter sw = new StreamWriter(fs1);
                            sw.WriteLine(sStuName "不存在");//开始写入值
                            sw.Close();
                            fs1.Close();
                        }
                        else
                        {
                            FileStream fs2 = new FileStream(txt_m.Text   "/"   "Log"   DateTime.Today.ToString("yyyy-MM-dd")   ".txt"   "", FileMode.Append, FileAccess.Write);
                            StreamWriter sr = new StreamWriter(fs2);
                            sr.WriteLine(sStuName   "不存在");//开始写入值
                            sr.Close();
                            fs2.Close();
                        }
                    }

                    if (progressBar1.Value < 98)
                    { progressBar1.Value  = progressBar1.Step; }
                   
                   
                  




                }

                fs.Close();
                progressBar1.Value = 100;             
                label4.Text = "完成";
              
                MessageBox.Show("完成");
                label4.Visible = false;
                progressBar1.Visible = false;
            }
            else
            {
                MessageBox.Show("请完成选择");
            }



        }

        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.GetFileName(labFiles[i]) != ".lab")//排除.lab文件
                        {
                            File.Copy(sourcePath   "\\"   Path.GetFileName(labFiles[i]), savePath   "\\"   Path.GetFileName(labFiles[i]), true);
                        }
                    }
                }
                if (labDirs.Length > 0)
                {
                    for (int j = 0; j < labDirs.Length; j  )
                    {
                        Directory.GetDirectories(sourcePath   "\\"   Path.GetFileName(labDirs[j]));

                        //递归调用
                        CopyOldLabFilesToNewLab(sourcePath   "\\"   Path.GetFileName(labDirs[j]), savePath   "\\"   Path.GetFileName(labDirs[j]));
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            #endregion
            return true;
        }


    }
}