Copy大文件时,显示进度条

int  totalSize;   // Total Size
         int  position;    // 每次向新文件写入数据后的位置
         const   int  BUFFER_SIZE  =   4096
        
byte [] buffer;
        Stream stream;

        
private   void  btnCopy_Click( object  sender, System.EventArgs e)
        
{
            
string strFile = "";

            OpenFileDialog dlg 
= new OpenFileDialog();
            
if ( dlg.ShowDialog() ==  DialogResult.OK )
            
{
                strFile 
= dlg.FileName ;
            }

            
else
            
{
                
return ;
            }


            FileStream fs 
= new FileStream( strFile , FileMode.Open , FileAccess.Read ) ;
            totalSize 
= (int)fs.Length ;
            stream 
= fs;

            
//Delete file which aready exists.
            if ( File.Exists( "D:/123.txt" ) )
                File.Delete( 
"D:/123.txt" );
             
//Copy file while larger than 4KB.



            
if ( totalSize > BUFFER_SIZE )
            
{
                buffer 
= new byte[ BUFFER_SIZE ]; 

                
// 将数据异步读取到buffer中
                stream.BeginRead( buffer , 0 , BUFFER_SIZE , new AsyncCallback( AsyncCopyFile ) , null );   
            }

            
else
            
{
                fs.Close();
            }

        }

        
        
/// <summary>
        
/// Asynchronously copy file
        
/// </summary>
        
/// <param name="ar"></param>

         private   void  AsyncCopyFile( IAsyncResult ar )
        
{
            
int readedLength ;
   
            
// Lock FileStream
            lock( stream )
            
{
                readedLength 
= stream.EndRead( ar );   // When stream endread, get readed length
            }


            
// Write to disk
            FileStream fsWriter = new FileStream( "D:/123.txt" , FileMode.Append , FileAccess.Write );
            fsWriter.Write( buffer , 
0 , buffer.Length );
            fsWriter.Close();
 
            
// Current stream position 
            position += readedLength;
     

            
// 通过系统委托更新前台进度条
            MethodInvoker m = new MethodInvoker( SynchProgressBar );
            m.BeginInvoke( 
null , null );
 
            
if ( position >= totalSize ) // Read over.
            {
                stream.Close();        
//Close FileStream  
                return ;
            }


            
// Continue to read and write
            lock ( stream )
            
{
                
int leftSize = totalSize - position;

                
if ( leftSize < BUFFER_SIZE ) 
                    buffer 
= new byte[ leftSize ];
                                                                                     
//以递归的方式继续异步读取后执行AsyncCopyFile操作
                stream.BeginRead( buffer , 0 , buffer.Length , new AsyncCallback( AsyncCopyFile ) , null );     
      
            }

        }


        
private   void  SynchProgressBar()
        
{
            
this.progressBar1.Maximum = totalSize;
            
this.progressBar1.Value   = position ; 
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值