设计器代码:

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

最新文章

  1. 转: BAT等研发团队的技术博客
  2. c#实现查询程序运行线程数
  3. ACM 6174问题
  4. cocos2dx 3.x(动态改变精灵的背景图片)
  5. [再做01背包] POJ 3624 Charm Bracelet
  6. 关于lab4实验git+近期出国手续办理
  7. 各加密模式的演示(ECB,CBC)
  8. Struts2 文件的上传与下载
  9. 回溯算法————n皇后、素数串
  10. 【课程分享】基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构、自己定义工作流)
  11. php中var_dump() 打印出一个对象的时候,信息怎么看?
  12. ubus socket always in connecting status
  13. Oracle数据库ORA-01109 数据库未打开
  14. Codeforces Round #525 (Div. 2) E. Ehab and a component choosing problem 数学
  15. XmlHelpers
  16. $interpolateProvider
  17. TurtleBot教程
  18. sublime常用方法
  19. opencv 启动摄像头 C++
  20. [Oracle]Oracle数据库CPU利用率很高解决方案

热门文章

  1. 自定义 Collection View 布局
  2. SVN各种错误提示产生原因及处理方法大全(转)
  3. JQuery IN ACTION读书笔记之一: JQuery选择器
  4. F - Goldbach`s Conjecture 对一个大于2的偶数n,找有多少种方法使两个素数的和为n;保证素数a&lt;=b; a+b==n; a,b都为素数。
  5. C指针类型小结
  6. 我的第二个java程序 循环
  7. [搬家]新域名 akagi201.org
  8. websocket数据流解析
  9. cocos2d-x 游戏优化方案
  10. 《从零开始学Swift》学习笔记(Day 14)——字符串的插入、删除和替换