C#文件夹复制的类

单线程方式,复制过程使用事件。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace Zgke.Copy
  6. {
  7.     /// <summary>
  8.     /// 文件甲复制
  9.     /// [email protected]
  10.     /// </summary>
  11.     public class CopyDirectory
  12.     {
  13.         /* 使用方法
  14.         private void button1_Click(object sender, EventArgs e)
  15.         {
  16.             Zgke.Copy.CopyDirectory _Info = new Zgke.Copy.CopyDirectory(@"F:/项目文件/产品化/联络中心V1.4.0/Source", @"E:/Temp");
  17.             _Info.MyCopyRun += new Zgke.Copy.CopyDirectory.CopyRun(_Info_MyCopyRun);
  18.             _Info.MyCopyEnd += new Zgke.Copy.CopyDirectory.CopyEnd(_Info_MyCopyEnd);
  19.             _Info.StarCopy();
  20.         }
  21.         void _Info_MyCopyEnd()
  22.         {
  23.             MessageBox.Show("复制完成");
  24.         }
  25.         void _Info_MyCopyRun(int FileCount, int CopyCount, long FileSize, long CopySize, string FileName)
  26.         {
  27.             this.Invoke((MethodInvoker)delegate {
  28.                 progressBar1.Maximum = FileCount;
  29.                 progressBar1.Value = CopyCount;
  30.                 label1.Text = CopySize.ToString() + "/" + FileSize.ToString();
  31.                 label2.Text = FileName;
  32.             });            
  33.         }
  34.         */
  35.         /// <summary>
  36.         /// 源目录
  37.         /// </summary>
  38.         private DirectoryInfo _Source;     
  39.         /// <summary>
  40.         /// 目标目录
  41.         /// </summary>
  42.         private DirectoryInfo _Target;
  43.    
  44.         /// <summary>
  45.         /// 文件复制完成
  46.         /// </summary>
  47.         /// <param name="FileCount">文件数量合计</param>
  48.         /// <param name="CopyCount">复制完成的数量</param>
  49.         /// <param name="FileSize">文件大小合计</param>
  50.         /// <param name="CopySize">复制完成的大小</param>
  51.         /// <param name="FileName">复制完成的文件名</param>
  52.         public delegate void CopyRun(int FileCount,int CopyCount,long FileSize,long CopySize,string FileName);
  53.         public event CopyRun MyCopyRun;
  54.         /// <summary>
  55.         /// 复制完成
  56.         /// </summary>
  57.         public delegate void CopyEnd();
  58.         public event CopyEnd MyCopyEnd;
  59.         private int _FileCount = 0;
  60.         private int _CopyCount = 0;
  61.         private long _FileSize = 0;
  62.         private long _CopySize = 0;
  63.           
  64.         /// <summary>
  65.         /// 复制目录包含文件
  66.         /// </summary>
  67.         /// <param name="p_SourceDirectory">源目录</param>
  68.         /// <param name="p_TargetDirectory">目标目录</param>
  69.         public CopyDirectory(string p_SourceDirectory,string p_TargetDirectory)
  70.         {
  71.             _Source = new DirectoryInfo(p_SourceDirectory);
  72.             _Target = new DirectoryInfo(p_TargetDirectory);
  73.             FileSystemInfo[] Temp = _Source.GetFileSystemInfos();           
  74.         }
  75.       
  76.         /// <summary>
  77.         /// 开始复制
  78.         /// </summary>
  79.         public void StarCopy()
  80.         {
  81.             GetFile(_Source);
  82.             System.Threading.Thread Th = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
  83.             Th.Start();
  84.         }
  85.         private void Run()
  86.         {           
  87.             Copy(_Source,_Target );
  88.             if (MyCopyEnd != null) MyCopyEnd();
  89.         }
  90.         /// <summary>
  91.         /// 复制目录到指定目录
  92.         /// </summary>
  93.         /// <param name="source">源目录</param>
  94.         /// <param name="target">目标目录</param>
  95.         private void GetFile(DirectoryInfo MySiurceDirectory)
  96.         {
  97.             foreach (FileInfo _File in MySiurceDirectory.GetFiles())    //循环文件   
  98.             {               
  99.                 _FileCount++;
  100.                 _FileSize += _File.Length;
  101.             }
  102.             foreach (DirectoryInfo _SourceSubDir in MySiurceDirectory.GetDirectories())  //循环子目录
  103.             {
  104.                 GetFile(_SourceSubDir);
  105.             }
  106.         }
  107.        
  108.         /// <summary>
  109.         /// 复制目录到指定目录
  110.         /// </summary>
  111.         /// <param name="source">源目录</param>
  112.         /// <param name="target">目标目录</param>
  113.         private void Copy(DirectoryInfo p_Source, DirectoryInfo p_Target)
  114.         {          
  115.             if (!Directory.Exists(p_Target.FullName))Directory.CreateDirectory(p_Target.FullName);                 
  116.             foreach (FileInfo _File in p_Source.GetFiles())       //循环文件 
  117.             {               
  118.                 _File.CopyTo(Path.Combine(p_Target.ToString(), _File.Name), true);               
  119.                 _CopyCount++;
  120.                 _CopySize += _File.Length;
  121.                 if (MyCopyRun != null) MyCopyRun(_FileCount, _CopyCount, _FileSize, _CopySize, _File.Name);
  122.             }
  123.             foreach (DirectoryInfo _SourceSubDir in p_Source.GetDirectories())  //循环子目录
  124.             {
  125.                 DirectoryInfo _NextDir = p_Target.CreateSubdirectory(_SourceSubDir.Name);
  126.                 Copy(_SourceSubDir, _NextDir);
  127.             }
  128.         }
  129.        
  130.     }
  131. }

你可能感兴趣的:(C#,null,产品)