zoukankan      html  css  js  c++  java
  • 大文件Copy

     

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

  • 相关阅读:
    Polly
    Ember.js 的视图层
    Ember.js入门教程、博文汇总
    点燃圣火! Ember.js 的初学者指南
    ElasticSearch第一步-环境配置
    C#泛型文章汇总
    LINQ之路10:LINQ to SQL 和 Entity Framework(下)
    LINQ之路 9:LINQ to SQL 和 Entity Framework(上)
    LINQ之路 8: 解释查询(Interpreted Queries)
    jquery.jqzoom.js图片放大镜
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/1794095.html
Copyright © 2011-2022 走看看