FileSteam实现文件复制+进度条

程序效果图:

 

程序关键代码:

 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.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.IO;
11 using System.Threading;
12 namespace FileProgressBars
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         /// <summary>
22         /// 选择文件
23         /// </summary>
24         private void button1_Click(object sender, EventArgs e)
25         {
26             OpenFileDialog op = new OpenFileDialog();
27             op.Title = "请选择要复制的文件";
28             op.InitialDirectory = @"C:\Users\Administrator\Desktop";
29             op.Filter = "所有文件|*.*";
30 
31             if (op.ShowDialog()==DialogResult.OK)
32             {
33                 txtSource.Text = op.FileName;
34             }
35 
36         }
37 
38         /// <summary>
39         /// 保存文件
40         /// </summary>
41         private void button2_Click(object sender, EventArgs e)
42         {
43             SaveFileDialog sd = new SaveFileDialog();
44             sd.Title = "请选择要保存的文件路径";
45             sd.InitialDirectory = @"C:\Users\Administrator\Desktop";
46             sd.Filter = "所有文件|*.*";
47 
48             if (sd.ShowDialog() != DialogResult.OK) return;
49             txtTarget.Text = sd.FileName;
50 
51             double wcl = 0;//完成率
52          
53             using (Stream fsRead=new FileStream(txtSource.Text,FileMode.Open,FileAccess.Read))
54             using (Stream fsWrite = new FileStream(txtTarget.Text, FileMode.Create, FileAccess.Write))
55             {
56                 progressBar1.Maximum = (int)fsRead.Length; //根据读取文件字节大小设置控件最大范围
57                 byte[] buffer = new byte[1024*1024*1]; //每次读1mb
58                 int readByteCount = 0;//实际读到的字节数
59 
60                 while (true)//读取文件流,直到读到的字节数为0时停止
61                 {
62                     readByteCount = fsRead.Read(buffer, 0, buffer.Length); //
63                     fsWrite.Write(buffer, 0, readByteCount);//
64 
65                     progressBar1.Value = (int)fsWrite.Length;//根据已读到的字节数设置进度条数值
66 
67                     //设置当前读取的百分比
68                     wcl = Convert.ToDouble(fsWrite.Length) / Convert.ToDouble(fsRead.Length) * 100; 
69                     label3.Text = ((int)wcl).ToString() + "%";
70 
71                     Thread.Sleep(100);//使程序挂起100毫秒,促使在客户端可以看到不断变化的过程(不设置的话程序不大的情况会一闪而过)
72 
73                     Application.DoEvents();//重点,必须加上,否则父子窗体都假死
74 
75                     if (readByteCount == 0) break; //读取完毕
76     
77                 }
78 
79                 label3.Text = "100%";
80 
81                 string debug = string.Empty;
82 
83             } // END Using
84 
85         }
86 
87        
88 
89 
90     }
91 }

 

练习目的:掌握FileStream使用缓冲器读写的方法。

 

posted @ 2020-06-18 16:32  姜承轩  阅读(634)  评论(0编辑  收藏  举报