多线程复制(1)

对C#的学习已经到了线程,觉得好神奇,自己模仿做的一个多线程复制,是学习以来最有成就感的一次,觉得找到了实实在在的用途,利用Visual Studio做的,窗体简陋了点,但是实实在在的能用,截图分享~~

多线程复制
 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 }

转载于:https://www.cnblogs.com/zyh-bg/archive/2012/08/08/2629158.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值