首页 > 代码库 > C# 学习黑马.Net视频教程,大文件拷贝
C# 学习黑马.Net视频教程,大文件拷贝
设计器代码:
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(28, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(299, 21); this.textBox1.TabIndex = 0; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(28, 51); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(299, 21); this.textBox2.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(334, 10); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(64, 23); this.button1.TabIndex = 1; this.button1.Text = "源文件"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(334, 49); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(64, 23); this.button2.TabIndex = 1; this.button2.Text = "保存到"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(40, 89); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(140, 48); this.button3.TabIndex = 2; 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(228, 89); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(132, 48); this.button4.TabIndex = 2; this.button4.Text = "大文件拷贝"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // progressBar1 // this.progressBar1.Location = new System.Drawing.Point(28, 164); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(350, 23); this.progressBar1.Step = 1; this.progressBar1.TabIndex = 3; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(384, 170); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(17, 12); this.label1.TabIndex = 4; this.label1.Text = "0%"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(26, 203); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(29, 12); this.label2.TabIndex = 4; 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(421, 234); 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;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 = http://www.mamicode.com/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 =http://www.mamicode.com/= 100) { System.Threading.Thread.Sleep(1000); //延迟1秒弹出 MessageBox.Show("拷贝成功!"); } }); } private void button4_Click(object sender, EventArgs e) { this.progressBar1.Value = http://www.mamicode.com/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 = http://www.mamicode.com/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 =http://www.mamicode.com/= 100) { System.Threading.Thread.Sleep(1000); //延迟1秒弹出 MessageBox.Show("拷贝成功!"); } }); } private void ShowProbar(object o, double m, double speed) { try { int barValue = http://www.mamicode.com/(int)Math.Floor(m) >= 100 ? 100 : (int)Math.Floor(m); this.Invoke(new System.Threading.WaitCallback((a) => { this.progressBar1.Value =http://www.mamicode.com/ 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); } } }}
C# 学习黑马.Net视频教程,大文件拷贝
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。