带进度条的文件拷贝

 带进度条的文件拷贝

  可以拷贝任何文件,拷贝过程有进度条实时反映,并记录使用时间.

  显示效果如图:


  private void btnCopyFile_Click(object sender, System.EventArgs e)
  {
   Nexsmart.MMS.FileManager.Class.CopyFileWorker cfw=new Nexsmart.MMS.FileManager.Class.CopyFileWorker();
   string SourceFile=txtSourceFile.Text.Trim();
   string AimFile=txtAimFile.Text.Trim();

   if(!System.IO.File.Exists(SourceFile))
   {
    MessageBox.Show("源文件不存在","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
    if(txtSourceFile.CanFocus)
     txtSourceFile.Focus();
    return ;
   }

   if(false)//!CheckFilePath)
   {
    MessageBox.Show("目标文件路径不正确!","",MessageBoxButtons.OK,MessageBoxIcon.Warning);
    if(txtAimFile.CanFocus)
     txtAimFile.Focus();
    return ;
   }

   cfw.SourceFile=txtSourceFile.Text;
   cfw.AimFile=txtAimFile.Text;

   cfw.OnCopyFile+=new Nexsmart.MMS.FileManager.Class.CopyFileWorker.CopyFileEventHandler(cfw_OnCopyFile);
   cfw.WorkOvered+=new Nexsmart.MMS.FileManager.Class.CopyFileWorker.WorkOverEventHandler(cfw_WorkOvered);

   System.Threading.Thread thread=new System.Threading.Thread(new  System.Threading.ThreadStart(cfw.CopyFile));//后台线程
   thread.Start();
   timer1.Enabled=true;//启动计数器
   timer1.Start();
   
  }

  private void cfw_OnCopyFile(long lngHad, long lngCount,string strShow)
  {
   this.pbarCopyFile.Maximum=(int)lngCount;
   this.pbarCopyFile.Value=(int)lngHad;

   this.lblHad.Text= Convert.ToString(lngHad/1024)+" KB";
   this.lblCount.Text=Convert.ToString(lngCount/1024)+" KB"; 
 
   lblShow.Text=strShow;
  }

  private void cfw_WorkOvered()
  {
   this.pbarCopyFile.Value=0;
   this.pbarCopyFile.Visible=false;
   SoureFile="";
   timer1.Enabled=false;
   timer1.Stop();//关闭计数器
   elapse=0;//清零计数变量
   lblShow.Text="";//清空显示
   MessageBox.Show("文件复制完成","",MessageBoxButtons.OK,MessageBoxIcon.Information);
   
  }
  private void timer1_Tick(object sender, System.EventArgs e)
  {
   ++elapse;
   lblTime.Text=elapse.ToString()+" 秒";
  }

//实现类

public class CopyFileWorker
 {
  public delegate void CopyFileEventHandler(long lngHad,long lngCount,string strShow);//定义一个委托
  public event CopyFileEventHandler OnCopyFile;//定义一个事件,在Copy文件时触发
  
  public delegate void WorkOverEventHandler();//定义一个委托
  public event WorkOverEventHandler WorkOvered;//定义一个事件,在Copy文件完成时触发

  public CopyFileWorker()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  public CopyFileWorker(string source,string aim)
  {
   _sourceFile=source;
   _aimFile=aim;
  }
  private string _sourceFile;//源文件路径
  public string SourceFile
  {
   get
   {
    return  _sourceFile;
   }
   set
   {
    _sourceFile=value;
   }
  }
  private string _aimFile;//目标文件路径
  public string AimFile
  {
   get
   {
    return _aimFile;
   }
   set
   {
    _aimFile=value;
   }
  }
  /// <summary>
  /// 二进制读取文件,任何文件
  /// </summary>
  public void CopyFile()
  {
   byte[] bytTemp=new byte[4096];//字节数组

   long lngHad=0;
   long lngCount;
   int z=5000;
   
   //源文件流
   System.IO.FileStream fsSource=new System.IO.FileStream(SourceFile,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read,4);
   //二进制读取器
   System.IO.BinaryReader bRead=new System.IO.BinaryReader(fsSource);
   //定位源文件流的头部
   bRead.BaseStream.Seek(0,System.IO.SeekOrigin.Begin);
   if(fsSource.Position>0)
    fsSource.Position=0;

   lngCount=fsSource.Length;

   if(System.IO.File.Exists(AimFile))
    System.IO.File.Delete(AimFile);

   //目标文件流
   System.IO.FileStream fsAim=new System.IO.FileStream(AimFile,System.IO.FileMode.CreateNew,System.IO.FileAccess.Write,System.IO.FileShare.Write,4);
   //二进制写入器
   System.IO.BinaryWriter bWrite=new System.IO.BinaryWriter(fsAim);

   while(z>=4096)
   {
    z=(int)bRead.Read(bytTemp,0,bytTemp.Length);//读入字节数组,返回读取的字节数量,如果小于4096,则到了文件尾
    bWrite.Write(bytTemp,0,bytTemp.Length);//从字节数组写入目标文件流
    
    lngHad+=z;
    string show="从"+SourceFile+"到"+AimFile;
    OnCopyFile(lngHad,lngCount,show);//触发事件 来控制主线程 这里是进度条和已完成复制文件字节显示   
   }

   bWrite.Flush();//清理缓存区

   bWrite.Close();
   bRead.Close();

   fsAim.Close();
   fsSource.Close();

   WorkOvered();//触发事件,调用完成复制后的处理程序

  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值