private void button3_Click(object sender, EventArgs e)
{
Thread thread = null;
//为了不让界面死掉,要将该操作放在一个线程中
thread = new Thread
(
() =>
{
//告诉系统不去检测非法的跨线程调用
CheckForIllegalCrossThreadCalls = false;
//创建一个文件流指向源文件
FileStream fsRead = new FileStream(this.textBox1.Text, FileMode.Open);
//创建一个文件流指向目标文件
FileStream fsWrite = new FileStream(this.textBox2.Text, FileMode.Create);
//记录一下该文件的长度
long fileLength = fsRead.Length;
//定义一个1M的缓冲区
byte[] buffer = new byte[1024 * 1024];
//先读取一次,并且将读取到的真正内容长度记录下来
int readLength = fsRead.Read(buffer, 0, buffer.Length);
//用来记录已经将多少内容写入到了文件中
long readCount = 0;
//只要读取到的内容不为0就接着读
while (readLength!=0)
{
//将前面已经读取到内存中的数据写入到文件中
fsWrite.Write(buffer ,0,readLength);
//已经读取的数量累加
readCount += readLength;
//计算已经读取的数据百分比
int percentage =(int)( readCount * 100 / fileLength);
this.progressBar1.Value = percentage;
//进行下一次读取
readLength = fsRead.Read(buffer, 0, buffer.Length);
}
fsRead.Close();
fsWrite.Close();
//清空缓冲区
buffer = null;
//回收一下内存
GC.Collect();
thread.Abort();
}
);
thread.Start();
}