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 ;
}
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 ;
}