Private void button3_Click (object sender, EventArgs e)
{
Thread thread = null;
// Put the operation in a thread to prevent the interface from dying
Thread = new Thread
(
() =>
{
// Tell the system not to detect illegal cross-thread calls
Checkforillegalcrossthreadcils = false;
// Create a file stream pointing to the source file
FileStream fsRead = new FileStream (this. textBox1.Text, FileMode. Open );
// Create a file stream pointing to the target file
FileStream fsWrite = new FileStream (this. textBox2.Text, FileMode. Create );
// Record the object Length
Long fileLength = fsRead. Length;
// Define a 1 m buffer.
Byte [] buffer = new byte [1024*1024];
// Read the content once and record the actual content length.
Int readLength = fsRead. Read (buffer, 0, buffer. Length );
// Used to record how much content has been written to the file
Long readCount = 0;
// Read as long as the read content is not 0
While (readLength! = 0)
{
// Write the data that has been read to the memory to the file
FsWrite. Write (buffer, 0, readLength );
// Accumulate the read count
ReadCount + = readLength;
// Calculate the percentage of read data
Int percentage = (int) (readCount * 100/fileLength );
This. progressBar1.Value = percentage;
// Perform the next read
ReadLength = fsRead. Read (buffer, 0, buffer. Length );
}
FsRead. Close ();
FsWrite. Close ();
// Clear the buffer
Buffer = null;
// Reclaim the memory
GC. Collect ();
Thread. Abort ();
}
);
Thread. Start ();
}