多线程拷贝

今天写一个我们复制文件时常用到的小程序,弄清其中多线程的原理,你就不会再烦恼为什么边复制,窗体还能边动态显示进程、在复制过程还能移动窗口等异步显示的问题,希望能对大家有所帮助。废话就不多说了,直接上图:

开始要做简单的逻辑判断,源文件路径为空或者不存在的情况下,要弹出友情提示:

接着判断目标路径是否已经存在,若不存在则自动创建文件,然后执行复制操作。若存在,需要请求用户是否覆盖原来文件,根据用户选择,执行后续操作:

有了前面的逻辑分析和明确的思路,接下来代码就小意思喽……

View Code
 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 namespace WindowsFormsApplication1
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19             CheckForIllegalCrossThreadCalls = false;
20         }
21 
22         private void CopyBtn_Click(object sender, EventArgs e)
23         {
24             Thread thread = new Thread(this.CopyFile);
25             thread.Start();
26         }
27         private void CopyFile()
28         {
29             string sfile = this.SourceTbox.Text.Trim();
30             string mfile = this.TargetTbox.Text.Trim();
31             if (!(File.Exists(sfile)))
32             {
33                 MessageBox.Show("源文件不存在");
34                 return;
35             }
36             FileStream readStream = File.Open(sfile, FileMode.Open);
37             FileStream writeStream = null;
38             if ((File.Exists(mfile)))
39             {
40                 DialogResult dr = MessageBox.Show("是否要覆盖文件", "警告", MessageBoxButtons.OKCancel);
41                 if (dr == DialogResult.OK)
42                 {
43                     writeStream = File.Open(mfile, FileMode.Truncate);
44                 }
45                 else
46                 {
47                     return;
48                 }
49             }
50             else
51             {
52                 writeStream = File.Open(mfile, FileMode.Create);
53             }
54             byte[] buffer = new byte[1024];
55             int i = 0;
56             long count = readStream.Length;
57             int ri = 0;
58             string progress = "";
59             while ((i = readStream.Read(buffer, 0, buffer.Length)) != 0)
60             {
61                 writeStream.Write(buffer, 0, i);
62                 ri = ri + i;
63                 progress = ((double)ri / count * 100).ToString("0.0") + "%";
64                 this.ProgressLab.Text = "进度: " + progress;
65                 this.CountLab.Text = "文件大小: " + ((double)count / (1024 * 1024)).ToString("0.0") + "Mb";
66                 this.progressBar.Maximum = (int)count;
67                 this.progressBar.Value = ri;
68                 if (ri == count)
69                 {
70                     this.progressBar.Value = 0;
71                 }
72             }
73             writeStream.Close();
74             readStream.Close();
75         }
76     }
77 }

以上代码比较直观,所以就没有写什么注释,还请大家谅解哈。有了以上简单的几个步骤,你就拥有了自己简单的多线程拷贝小程序了,虽然简陋了点儿,可毕竟是自己设计的,对它内部原理也肯定有比较深刻的掌握,况且其复制,拖动,显示进程和文件总大小的功能也都具备,所以还应算作一个差强人意的复制小程序……

转载于:https://www.cnblogs.com/yunqinglin/archive/2012/08/04/2623390.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值