设计器代码:

  1. namespace 大文件拷贝
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// 必需的设计器变量。
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// 清理所有正在使用的资源。
  12. /// </summary>
  13. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows 窗体设计器生成的代码
  24.  
  25. /// <summary>
  26. /// 设计器支持所需的方法 - 不要
  27. /// 使用代码编辑器修改此方法的内容。
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.textBox1 = new System.Windows.Forms.TextBox();
  32. this.textBox2 = new System.Windows.Forms.TextBox();
  33. this.button1 = new System.Windows.Forms.Button();
  34. this.button2 = new System.Windows.Forms.Button();
  35. this.button3 = new System.Windows.Forms.Button();
  36. this.button4 = new System.Windows.Forms.Button();
  37. this.progressBar1 = new System.Windows.Forms.ProgressBar();
  38. this.label1 = new System.Windows.Forms.Label();
  39. this.label2 = new System.Windows.Forms.Label();
  40. this.SuspendLayout();
  41. //
  42. // textBox1
  43. //
  44. this.textBox1.Location = new System.Drawing.Point(, );
  45. this.textBox1.Name = "textBox1";
  46. this.textBox1.Size = new System.Drawing.Size(, );
  47. this.textBox1.TabIndex = ;
  48. //
  49. // textBox2
  50. //
  51. this.textBox2.Location = new System.Drawing.Point(, );
  52. this.textBox2.Name = "textBox2";
  53. this.textBox2.Size = new System.Drawing.Size(, );
  54. this.textBox2.TabIndex = ;
  55. //
  56. // button1
  57. //
  58. this.button1.Location = new System.Drawing.Point(, );
  59. this.button1.Name = "button1";
  60. this.button1.Size = new System.Drawing.Size(, );
  61. this.button1.TabIndex = ;
  62. this.button1.Text = "源文件";
  63. this.button1.UseVisualStyleBackColor = true;
  64. this.button1.Click += new System.EventHandler(this.button1_Click);
  65. //
  66. // button2
  67. //
  68. this.button2.Location = new System.Drawing.Point(, );
  69. this.button2.Name = "button2";
  70. this.button2.Size = new System.Drawing.Size(, );
  71. this.button2.TabIndex = ;
  72. this.button2.Text = "保存到";
  73. this.button2.UseVisualStyleBackColor = true;
  74. this.button2.Click += new System.EventHandler(this.button2_Click);
  75. //
  76. // button3
  77. //
  78. this.button3.Location = new System.Drawing.Point(, );
  79. this.button3.Name = "button3";
  80. this.button3.Size = new System.Drawing.Size(, );
  81. this.button3.TabIndex = ;
  82. this.button3.Text = "1个1个字节拷贝";
  83. this.button3.UseVisualStyleBackColor = true;
  84. this.button3.Click += new System.EventHandler(this.button3_Click);
  85. //
  86. // button4
  87. //
  88. this.button4.Location = new System.Drawing.Point(, );
  89. this.button4.Name = "button4";
  90. this.button4.Size = new System.Drawing.Size(, );
  91. this.button4.TabIndex = ;
  92. this.button4.Text = "大文件拷贝";
  93. this.button4.UseVisualStyleBackColor = true;
  94. this.button4.Click += new System.EventHandler(this.button4_Click);
  95. //
  96. // progressBar1
  97. //
  98. this.progressBar1.Location = new System.Drawing.Point(, );
  99. this.progressBar1.Name = "progressBar1";
  100. this.progressBar1.Size = new System.Drawing.Size(, );
  101. this.progressBar1.Step = ;
  102. this.progressBar1.TabIndex = ;
  103. //
  104. // label1
  105. //
  106. this.label1.AutoSize = true;
  107. this.label1.Location = new System.Drawing.Point(, );
  108. this.label1.Name = "label1";
  109. this.label1.Size = new System.Drawing.Size(, );
  110. this.label1.TabIndex = ;
  111. this.label1.Text = "0%";
  112. //
  113. // label2
  114. //
  115. this.label2.AutoSize = true;
  116. this.label2.Location = new System.Drawing.Point(, );
  117. this.label2.Name = "label2";
  118. this.label2.Size = new System.Drawing.Size(, );
  119. this.label2.TabIndex = ;
  120. this.label2.Text = "0k/s";
  121. //
  122. // Form1
  123. //
  124. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  125. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  126. this.ClientSize = new System.Drawing.Size(, );
  127. this.Controls.Add(this.label2);
  128. this.Controls.Add(this.label1);
  129. this.Controls.Add(this.progressBar1);
  130. this.Controls.Add(this.button4);
  131. this.Controls.Add(this.button3);
  132. this.Controls.Add(this.button2);
  133. this.Controls.Add(this.button1);
  134. this.Controls.Add(this.textBox2);
  135. this.Controls.Add(this.textBox1);
  136. this.Name = "Form1";
  137. this.Text = "Form1";
  138. this.ResumeLayout(false);
  139. this.PerformLayout();
  140.  
  141. }
  142.  
  143. #endregion
  144.  
  145. private System.Windows.Forms.TextBox textBox1;
  146. private System.Windows.Forms.TextBox textBox2;
  147. private System.Windows.Forms.Button button1;
  148. private System.Windows.Forms.Button button2;
  149. private System.Windows.Forms.Button button3;
  150. private System.Windows.Forms.Button button4;
  151. private System.Windows.Forms.ProgressBar progressBar1;
  152. private System.Windows.Forms.Label label1;
  153. private System.Windows.Forms.Label label2;
  154. }
  155. }

代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. //*************************************************************************************
  13. //**********************黑马程序员/Asp.Net/技术群:118041544 **************************
  14. //*************************************************************************************
  15. namespace 大文件拷贝
  16. {
  17. public partial class Form1 : Form
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. private string _fileName;
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. OpenFileDialog ofd = new OpenFileDialog();
  27. if (ofd.ShowDialog() == DialogResult.OK)
  28. {
  29. this.textBox1.Text = ofd.FileName;
  30. _fileName = ofd.SafeFileName;
  31. }
  32. }
  33.  
  34. private void button2_Click(object sender, EventArgs e)
  35. {
  36. FolderBrowserDialog fbd = new FolderBrowserDialog();
  37. if (fbd.ShowDialog() == DialogResult.OK)
  38. {
  39. this.textBox2.Text = fbd.SelectedPath;
  40. }
  41. }
  42.  
  43. private void button3_Click(object sender, EventArgs e)
  44. {
  45. this.progressBar1.Value = ;
  46. this.label1.Text = "0%";
  47. if (!File.Exists(this.textBox1.Text))
  48. {
  49. MessageBox.Show("找不到目标文件!");
  50. return;
  51. }
  52. if (!Directory.Exists(this.textBox2.Text))
  53. {
  54. MessageBox.Show("请选择有效的保存路径!");
  55. return;
  56. }
  57. string fileRead = this.textBox1.Text;
  58. string fileSave = this.textBox2.Text + "/" + _fileName;
  59.  
  60. System.Threading.ThreadPool.QueueUserWorkItem((o) =>
  61. {
  62. using (IDisposable file = new FileStream(fileRead, FileMode.Open, FileAccess.Read),
  63. fileWrite = new FileStream(fileSave, FileMode.Create, FileAccess.Write))
  64. {
  65. int count = ;
  66. long fileLength = ((FileStream)file).Length; //目标文件大小
  67. double n = ( * 1.0 / fileLength); //每次进度条累加
  68. double m = ; //累加统计
  69. int spam = ; //时间间隔
  70. double speed = ; //拷贝速度
  71.  
  72. do
  73. {
  74. DateTime time = DateTime.Now;
  75. count = ((FileStream)file).ReadByte();
  76. ((FileStream)fileWrite).WriteByte((byte)count);//按一个字节一个字节写入
  77. //计算速度 单位k/s
  78. speed = (1.0 * / ) / spam;
  79.  
  80. //进度条
  81. m = m + n;
  82.  
  83. //控件显示
  84. ShowProbar(o, m, speed);
  85.  
  86. //保存时间间隔,单位毫秒
  87. spam = (DateTime.Now - time).Milliseconds > ? (DateTime.Now - time).Milliseconds : spam;//必须大于0
  88.  
  89. } while (count != -);
  90. }
  91. if (this.progressBar1.Value == )
  92. {
  93. System.Threading.Thread.Sleep(); //延迟1秒弹出
  94. MessageBox.Show("拷贝成功!");
  95. }
  96. });
  97. }
  98.  
  99. private void button4_Click(object sender, EventArgs e)
  100. {
  101. this.progressBar1.Value = ;
  102. this.label1.Text = "0%";
  103. if (!File.Exists(this.textBox1.Text))
  104. {
  105. MessageBox.Show("找不到目标文件!");
  106. return;
  107. }
  108. if (!Directory.Exists(this.textBox2.Text))
  109. {
  110. MessageBox.Show("请选择有效的保存路径!");
  111. return;
  112. }
  113. string fileRead = this.textBox1.Text;
  114. string fileSave = Path.Combine(this.textBox2.Text, _fileName);
  115. System.Threading.ThreadPool.QueueUserWorkItem((o) =>
  116. {
  117. using (IDisposable file = new FileStream(fileRead, FileMode.Open, FileAccess.Read),
  118. fileWrite = new FileStream(fileSave, FileMode.Create, FileAccess.Write))
  119. {
  120. int count = ;
  121. long fileLength =((FileStream)file).Length; //目标文件大小
  122. //根据目标文件大小创建byte数组长度
  123. byte[] data = new byte[fileLength > * * ? * * : fileLength]; //30M 1024 * 1024 * 30
  124. int step = (int)Math.Ceiling(fileLength * 1.0 / data.Length); //分流段数
  125. double n = ( * 1.0 / step); //每次进度条累加
  126. double m = ; //累加统计
  127. int spam = ; //时间间隔
  128. double speed = ; //拷贝速度
  129. do
  130. {
  131. DateTime time = DateTime.Now;
  132. //文件流操作
  133. count = ((FileStream)file).Read(data, , data.Length);
  134. ((FileStream)fileWrite).Write(data, , count);
  135.  
  136. //保存时间间隔,单位毫秒
  137. spam = (DateTime.Now - time).Milliseconds > ? (DateTime.Now - time).Milliseconds : spam;//必须大于0
  138.  
  139. //计算速度 单位k/s
  140. if (count == data.Length)
  141. {
  142. speed = (count * 1.0 / / spam) * ;
  143. }
  144.  
  145. //进度条
  146. m = m + n;
  147.  
  148. //控件显示
  149. ShowProbar(o, m, speed);
  150.  
  151. } while (count > );
  152. }
  153. if (this.progressBar1.Value == )
  154. {
  155. System.Threading.Thread.Sleep(); //延迟1秒弹出
  156. MessageBox.Show("拷贝成功!");
  157. }
  158. });
  159. }
  160.  
  161. private void ShowProbar(object o, double m, double speed)
  162. {
  163. try
  164. {
  165. int barValue = (int)Math.Floor(m) >= ? : (int)Math.Floor(m);
  166.  
  167. this.Invoke((MethodInvoker)(() =>
  168. {
  169. this.progressBar1.Value = barValue;
  170. this.label1.Text = barValue.ToString() + "%";
  171. this.label2.Text = speed >= ? (speed / ).ToString("0.0m/s") : (speed >= ? speed.ToString("0k/s") : (speed * ).ToString("0b/s"));
  172. }));
  173. }
  174. catch (Exception e)
  175. {
  176. throw new Exception(e.Message);
  177. }
  178. }
  179. }
  180. }

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. SQL数据库查询练习题(更正版)

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  2. Matlab命令行版打开

    matlab -nosplash -nodesktop 运行文件:matlab  -nodesktop -nosplash -r file

  3. 第一百七十九节,jQuery-UI,知问前端--按钮 UI-图标

    jQuery-UI,知问前端--按钮 UI 学习要点: 1.使用 button 按钮 2.修改 button 样式 3.button()方法的属性 4.button('action', param) ...

  4. Eclipse 编译项目

    Eclipse 编译项目 编译 Java 项目 一个项目可以关联多个编译器. java 项目关联的是 java 编译器.可以通过以下方式来查看项目关联的编译器: 在 Package Explorer ...

  5. Eclipse 运行配置(Run Configuration)

    Eclipse 运行配置(Run Configuration) 创建和使用 Eclipse 运行配置 在运行配置(Run Configuration)对话框中可以创建多个运行配置.每个配置可以在应用中 ...

  6. hdu 1956(混合图的欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1956 思路:先将无向边定向,比如1<->3,可以定它的方向为1->3,1的出度++, ...

  7. 关于swift 单元测试的补充

    最近小弟在自己学习研究swift , 习惯于写一边写单元测试一边写逻辑的我来说,在xcode环境没有单元测试,写起来就是有个不是实在的感觉. 至于怎么创建单元测试.怎么写,不是这个文章的主题,因为看了 ...

  8. 强大的find命令

    find命令确实很强大,自己懒得计,转载一篇: 原文出处:http://roclinux.cn/?p=18 1. 想查看当前文件夹及子文件夹里有没有文件名为“abc”的文件 # find . -nam ...

  9. iOS-毛玻璃、navigationBar滑动颜色渐变

    1.毛玻璃实现 iOS8以上,官方提供了系统方法实现毛玻璃,代码如下: // iOS8 UIVisualEffectView UIImageView *bgView = [[UIImageView a ...

  10. S​Q​L​ ​S​e​r​v​e​r​ ​服务无法启动,错误1069解决办法

    刚刚接到公司服务器上的SQL Server服务无法启动的问题需要解决,在进入“SQL Server 配置管理器”启动MSSQLServer 服务时,启动过程异常缓慢预示着“异常”的存在.果然,系统给出 ...