Large file Copy

Source: Internet
Author: User

 

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.