对C#的学习已经到了线程,觉得好神奇,自己模仿做的一个多线程复制,是学习以来最有成就感的一次,觉得找到了实实在在的用途,利用Visual Studio做的,窗体简陋了点,但是实实在在的能用,截图分享~~
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 using System.Threading; 11 12 13 namespace ThreadCopy 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 CheckForIllegalCrossThreadCalls = false; 21 } 22 23 private void btnCopy_Click(object sender, EventArgs e) 24 { 25 Thread thread = new Thread(this.CopyFile); 26 thread.Start(); 27 } 28 private void CopyFile() 29 { 30 string sFile = this.txtSLB.Text.Trim(); 31 string mFile = this.txtMLB.Text.Trim(); 32 FileStream readstream = null; 33 FileStream writestream = null; 34 if (!File.Exists(sFile)) 35 { 36 MessageBox.Show("源文件不存在!"); 37 return; 38 } 39 // if (!File.Exists(mFile)) 40 //{ 41 // MessageBox.Show("移动地址不存在!"); 42 // return; 43 //} 44 using (readstream = File.Open(sFile, FileMode.Open)) 45 { 46 if (File.Exists(mFile)) 47 { 48 DialogResult dr = MessageBox.Show("是否要覆盖?", "警告", MessageBoxButtons.OKCancel); 49 if (dr == DialogResult.OK) 50 { 51 writestream = File.Open(mFile, FileMode.Truncate); 52 } 53 else 54 { 55 return; 56 } 57 } 58 else 59 { 60 writestream = File.Open(mFile, FileMode.Create); 61 } 62 using (writestream) 63 { 64 byte[] buffer = new byte[1024 * 4]; 65 int i = 0; 66 long count = readstream.Length; 67 int re = 0; 68 string ps = ""; 69 while ((i = readstream.Read(buffer, 0, buffer.Length)) != 0) 70 { 71 writestream.Write(buffer, 0, i); 72 re = re + i; 73 ps = ((double)re / count * 100).ToString("0.00") + "%"; 74 this.PC.Text = ps; 75 this.Count.Text = count.ToString(); 76 this.PB.Maximum = (int)count; 77 this.PB.Value = re; 78 if (re == count) 79 { 80 this.PB.Value = 0; 81 } 82 } 83 } 84 } 85 } 86 } 87 }