设计器代码:

namespace 大文件拷贝
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(, );
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(, );
this.textBox1.TabIndex = ;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(, );
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(, );
this.textBox2.TabIndex = ;
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "源文件";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(, );
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(, );
this.button2.TabIndex = ;
this.button2.Text = "保存到";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(, );
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(, );
this.button3.TabIndex = ;
this.button3.Text = "1个1个字节拷贝";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(, );
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(, );
this.button4.TabIndex = ;
this.button4.Text = "大文件拷贝";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(, );
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(, );
this.progressBar1.Step = ;
this.progressBar1.TabIndex = ;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "0%";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "0k/s";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}

代码:

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; //*************************************************************************************
//**********************黑马程序员/Asp.Net/技术群:118041544 **************************
//*************************************************************************************
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 = ;
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 = ;
long fileLength = ((FileStream)file).Length; //目标文件大小
double n = ( * 1.0 / fileLength); //每次进度条累加
double m = ; //累加统计
int spam = ; //时间间隔
double speed = ; //拷贝速度 do
{
DateTime time = DateTime.Now;
count = ((FileStream)file).ReadByte();
((FileStream)fileWrite).WriteByte((byte)count);//按一个字节一个字节写入
//计算速度 单位k/s
speed = (1.0 * / ) / spam; //进度条
m = m + n; //控件显示
ShowProbar(o, m, speed); //保存时间间隔,单位毫秒
spam = (DateTime.Now - time).Milliseconds > ? (DateTime.Now - time).Milliseconds : spam;//必须大于0 } while (count != -);
}
if (this.progressBar1.Value == )
{
System.Threading.Thread.Sleep(); //延迟1秒弹出
MessageBox.Show("拷贝成功!");
}
});
} private void button4_Click(object sender, EventArgs e)
{
this.progressBar1.Value = ;
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 = ;
long fileLength =((FileStream)file).Length; //目标文件大小
//根据目标文件大小创建byte数组长度
byte[] data = new byte[fileLength > * * ? * * : fileLength]; //30M 1024 * 1024 * 30
int step = (int)Math.Ceiling(fileLength * 1.0 / data.Length); //分流段数
double n = ( * 1.0 / step); //每次进度条累加
double m = ; //累加统计
int spam = ; //时间间隔
double speed = ; //拷贝速度
do
{
DateTime time = DateTime.Now;
//文件流操作
count = ((FileStream)file).Read(data, , data.Length);
((FileStream)fileWrite).Write(data, , count); //保存时间间隔,单位毫秒
spam = (DateTime.Now - time).Milliseconds > ? (DateTime.Now - time).Milliseconds : spam;//必须大于0 //计算速度 单位k/s
if (count == data.Length)
{
speed = (count * 1.0 / / spam) * ;
} //进度条
m = m + n; //控件显示
ShowProbar(o, m, speed); } while (count > );
}
if (this.progressBar1.Value == )
{
System.Threading.Thread.Sleep(); //延迟1秒弹出
MessageBox.Show("拷贝成功!");
}
});
} private void ShowProbar(object o, double m, double speed)
{
try
{
int barValue = (int)Math.Floor(m) >= ? : (int)Math.Floor(m); this.Invoke((MethodInvoker)(() =>
{
this.progressBar1.Value = barValue;
this.label1.Text = barValue.ToString() + "%";
this.label2.Text = speed >= ? (speed / ).ToString("0.0m/s") : (speed >= ? speed.ToString("0k/s") : (speed * ).ToString("0b/s"));
}));
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
}

C# 学习黑马.Net视频教程,大文件拷贝的更多相关文章

  1. C# IO操作(四)大文件拷贝(文件流的使用)、文件编码

         大文件拷贝(文件流的使用).文件编码 首先说一下大文件拷贝和文件流,因为计算机的内存资源是有限的,面对几个G甚至更大的文件,需要通过程序来完成拷贝,就需要用到文件流(因为我们无法做到把文件一 ...

  2. nodejs pipe实现大文件拷贝

    原文: http://nqdeng.github.io/7-days-nodejs/ --------------------------------------------------------- ...

  3. ios开发网络学习四:NSURLConnection大文件断点下载

    #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...

  4. JAVA-IO流大文件拷贝

    package com.test.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

  5. IO流大文件拷贝

                            String resourcesPath="f:/a.grd";          String targetPath=" ...

  6. 大文件拆分问题的java实践(附源码)

    引子 大文件拆分问题涉及到io处理.并发编程.生产者/消费者模式的理解,是一个很好的综合应用场景,为此,花点时间做一些实践,对相关的知识做一次梳理和集成,总结一些共性的处理方案和思路,以供后续工作中借 ...

  7. nodejs 文件拷贝

    小文件拷贝 我们使用NodeJS内置的fs模块简单实现这个程序如下. var fs = require('fs'); function copy(src, dst) { fs.writeFileSyn ...

  8. 大文件拆分方案的java实践(附源码)

    引子 大文件拆分问题涉及到io处理.并发编程.生产者/消费者模式的理解,是一个很好的综合应用场景,为此,花点时间做一些实践,对相关的知识做一次梳理和集成,总结一些共性的处理方案和思路,以供后续工作中借 ...

  9. node实现文件拷贝2

    https://www.cnblogs.com/coding4/p/7495968.html 文件拷贝NodeJS 提供了基本的文件操作 API,但是像文件拷贝这种高级功能就没有提供,因此我们先拿文件 ...

随机推荐

  1. Help Hanzo lightof 1197 求一段区间内素数个数,[l,r] 在 [1,1e9] 范围内。r-l<=1e5; 采用和平常筛素数的方法。平移区间即可。

    /** 题目:Help Hanzo lightof 1197 链接:https://vjudge.net/contest/154246#problem/M 题意:求一段区间内素数个数,[l,r] 在 ...

  2. 巧用批处理cmd快速切换IP地址

    如果你的笔记本经常在不同的地方使用,有些地方需要自动获取IP,而有些地方需要配置固定IP,每换一个地方都需要重新配置一遍,是不是感觉很麻烦呢? 下面介绍一种通过建立批处理文件来快速切换IP的方法: s ...

  3. Mybatis学习手记(一)

    年近不惑,为生活所迫,重新开始学习JAVA,计划将公司的开发平台统一到JAVA上来.现公司的技术杂乱,有PB开发的程序要维护.有PYTHON的程序要维护,有.NET的,也有JAVA的,非常不容易形成合 ...

  4. linq to sql 动态构建查询表达式树

    通过Expression类进行动态构造lamda表达式. 实现了以下几种类型,好了代码说话: public Expression<Func<T, bool>> GetAndLa ...

  5. linux 复制粘贴

    如果想把文件内的第三行内容黏贴到第十五行: 1.进入你的文件,(处于命令模式,而不是编辑模式) 2.将你的光标移到你的第三行的开头(用方向键) 3,按下v,(你发现切换到可视模式了),按方向键,知道选 ...

  6. ionic + cordova 环境搭建

    1.安装nodejs:官网下载安装包,双击安装即可.成功后在控制台输入node -v 显示版本号即成功. 2.安装Java,配置环境变量,下载安卓sdk ,配置环境变量 ANDROID_HOME 为s ...

  7. 初探webpack之环境配置

    先感叹一句,前端的发展真是太快了,ng和bb还没怎么学好就要过时了.现在感觉react当是未来的一个大方向. 以前一直用的grunt,不过前段时间作者已经停止更新了.正好webpack风头正盛,咱也不 ...

  8. js原型对象中属性被覆盖(1)

    /**   *@author 程无衣   *@description 关于在原型对象中属性被覆盖   */       function Person(){}       Person.prototy ...

  9. thymeleaf模板引擎

    thymeleaf模板引擎 thymeleaf是现代化服务器端的Java模板引擎,不同于JSP和FreeMarker,Thymeleaf的语法更加接近HTML,并且也有不错的扩展性.详细资料可以浏览官 ...

  10. SpringBoot整合Dubbo报错: java.lang.ClassCastException

    com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote proxy method queryGoodsLimitPage to regi ...