C# 学习黑马.Net视频教程,大文件拷贝
2020-12-13 15:05
                         标签:style   blog   http   io   color   ar   os   使用   for    设计器代码:   代码:   C# 学习黑马.Net视频教程,大文件拷贝 标签:style   blog   http   io   color   ar   os   使用   for    原文地址:http://www.cnblogs.com/han1982/p/4070360.html

namespace 大文件拷贝
{
    partial class Form1
    {
        /// 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 大文件拷贝
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string _fileName;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = ofd.FileName;
                _fileName = ofd.SafeFileName;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = fbd.SelectedPath;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            this.label1.Text = "0%";
            if (!File.Exists(this.textBox1.Text))
            {
                MessageBox.Show("找不到目标文件!");
                return;
            }
            if (!Directory.Exists(this.textBox2.Text))
            {
                MessageBox.Show("请选择有效的保存路径!");
                return;
            }
            string fileRead = this.textBox1.Text;
            string fileSave = this.textBox2.Text + "/" + _fileName;
            System.Threading.ThreadPool.QueueUserWorkItem((o) =>
            {
                using (IDisposable file = new FileStream(fileRead, FileMode.Open, FileAccess.Read),
                fileWrite = new FileStream(fileSave, FileMode.Create, FileAccess.Write))
                {
                    int count = 0;
                    long fileLength = ((FileStream)file).Length; //目标文件大小
                    double n = (100 * 1.0 / fileLength);  //每次进度条累加
                    double m = 0;                         //累加统计
                    int spam = 1;                         //时间间隔
                    double speed = 0;                     //拷贝速度
                    do
                    {
                        DateTime time = DateTime.Now;
                        count = ((FileStream)file).ReadByte();
                        ((FileStream)fileWrite).WriteByte((byte)count);//按一个字节一个字节写入
                        //计算速度 单位k/s 
                        speed = (1.0 * 1000 / 1024) / spam;
                        //进度条
                        m = m + n;
                        //控件显示
                        ShowProbar(o, m, speed);
                        //保存时间间隔,单位毫秒
                        spam = (DateTime.Now - time).Milliseconds > 0 ? (DateTime.Now - time).Milliseconds : spam;//必须大于0
                    } while (count != -1);
                }
                if (this.progressBar1.Value == 100)
                {
                    System.Threading.Thread.Sleep(1000); //延迟1秒弹出
                    MessageBox.Show("拷贝成功!");
                }
            });
        }
        private void button4_Click(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            this.label1.Text = "0%";
            if (!File.Exists(this.textBox1.Text))
            {
                MessageBox.Show("找不到目标文件!");
                return;
            }
            if (!Directory.Exists(this.textBox2.Text))
            {
                MessageBox.Show("请选择有效的保存路径!");
                return;
            }
            string fileRead = this.textBox1.Text;
            string fileSave = Path.Combine(this.textBox2.Text, _fileName);
            System.Threading.ThreadPool.QueueUserWorkItem((o) =>
            {
                using (IDisposable file = new FileStream(fileRead, FileMode.Open, FileAccess.Read),
                    fileWrite = new FileStream(fileSave, FileMode.Create, FileAccess.Write))
                {
                    int count = 0;
                    long fileLength =((FileStream)file).Length; //目标文件大小
                    //根据目标文件大小创建byte数组长度
                    byte[] data = new byte[fileLength > 1024 * 1024 * 30 ? 1024 * 1024 * 30 : fileLength]; //30M 1024 * 1024 * 30
                    int step = (int)Math.Ceiling(fileLength * 1.0 / data.Length); //分流段数
                    double n = (100 * 1.0 / step);  //每次进度条累加
                    double m = 0;                   //累加统计
                    int spam = 1;                   //时间间隔
                    double speed = 0;               //拷贝速度
                    do
                    {
                        DateTime time = DateTime.Now;
                        //文件流操作
                        count = ((FileStream)file).Read(data, 0, data.Length);
                        ((FileStream)fileWrite).Write(data, 0, count);
                        //保存时间间隔,单位毫秒
                        spam = (DateTime.Now - time).Milliseconds > 0 ? (DateTime.Now - time).Milliseconds : spam;//必须大于0
                        //计算速度 单位k/s 
                        if (count == data.Length)
                        {
                            speed = (count * 1.0 / 1024 / spam) * 1000;
                        }
                        //进度条
                        m = m + n;
                        //控件显示
                        ShowProbar(o, m, speed);
                        
                    } while (count > 0);  
                }
                if (this.progressBar1.Value == 100)
                {
                    System.Threading.Thread.Sleep(1000); //延迟1秒弹出
                    MessageBox.Show("拷贝成功!");
                }
            });
        }
        private void ShowProbar(object o, double m, double speed)
        {
            try
            {
                int barValue = (int)Math.Floor(m) >= 100 ? 100 : (int)Math.Floor(m);
                this.Invoke(new System.Threading.WaitCallback((a) =>
                {
                    this.progressBar1.Value = barValue;
                    this.label1.Text = barValue.ToString() + "%";
                    this.label2.Text = speed > 1024 ? (speed / 1024).ToString("0.0m/s") : (speed >= 1 ? speed.ToString("0k/s") : (speed * 1024).ToString("0b/s"));
                }), o);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}