c#根据文件大小显示文件复制进度条实例

  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 CopyProgress015
 13 {
 14 public partial class Form1 : Form
 15 {
 16     public Form1()
 17     {
 18  InitializeComponent();
 19     }
 20     private Thread thdCopyFile; //创建一个线程
 21     private string str = "";  //用来记录源文件的名字
 22     FileStream FormerOpenStream;  //实例化源文件FileStream类
 23     FileStream ToFileOpenStream;  //实例化目标文件FileStream类 
 24  
 25     #region //复制文件 函数
 26     /// <summary>
 27     /// 复制文件
 28     /// </summary>
 29     /// <param name="FormerFile">源文件路径</param>
 30     /// <param name="ToFile">目的文件路径</param>
 31     /// <param name="TranSize">传输大小</param>
 32     /// <param name="progressBar1">ProgressBar控件</param>
 33     public void CopyFile(string FormerFile, string ToFile, int TranSize, ProgressBar progressBar1)
 34     {
 35  progressBar1.Value = 0;//设置进度条的当前位置为0
 36  progressBar1.Minimum = 0; //设置进度条的最小值为0 
 37   try
 38  {
 39      FormerOpenStream = new FileStream(FormerFile, FileMode.Open, FileAccess.Read);//以只读方式打开源文件
 40  }
 41  catch (IOException ex)
 42  {
 43      MessageBox.Show(ex.Message);
 44      return;
 45  } 
 46   try
 47  {
 48      FileStream fileToCreate = new FileStream(ToFile, FileMode.Create); //创建目的文件,如果已存在将被覆盖
 49      fileToCreate.Close();//关闭所有fileToCreate的资源
 50      fileToCreate.Dispose();//释放所有fileToCreate的资源
 51  }
 52  catch(IOException ex)
 53  {
 54      MessageBox.Show(ex.Message);
 55      return;
 56  } 
 57  
 58        ToFileOpenStream = new FileStream(ToFile, FileMode.Append, FileAccess.Write);//以写方式打开目的文件 
 59  
 60        int max = Convert.ToInt32(Math.Ceiling((Double)FormerOpenStream.Length / (Double)TranSize));//根据一次传输的大小,计算最大传输个数. Math.Ceiling 方法 (Double),返回大于或等于指定的双精度浮点数的最小整数值。 
 61   progressBar1.Maximum = max;//设置进度条的最大值
 62  int FileSize; //每次要拷贝的文件的大小
 63  if (TranSize < FormerOpenStream.Length)  //如果分段拷贝,即每次拷贝内容小于文件总长度
 64  {
 65      byte[] buffer = new byte[TranSize]; //根据传输的大小,定义一个字节数组,用来存储传输的字节
 66      int copied = 0;//记录传输的大小
 67      int tem_n = 1;//设置进度栏中进度的增加个数
 68      while (copied <= ((int)FormerOpenStream.Length - TranSize))
 69      {
 70   FileSize = FormerOpenStream.Read(buffer, 0, TranSize);//从0开始读到buffer字节数组中,每次最大读TranSize
 71   FormerOpenStream.Flush();   //清空缓存
 72   ToFileOpenStream.Write(buffer, 0, TranSize); //向目的文件写入字节
 73   ToFileOpenStream.Flush();//清空缓存
 74   ToFileOpenStream.Position = FormerOpenStream.Position; //是源文件的目的文件流的位置相同
 75   copied += FileSize; //记录已经拷贝的大小
 76   progressBar1.Value = progressBar1.Value + tem_n; //增加进度栏的进度块
 77      }
 78      int leftSize = (int)FormerOpenStream.Length - copied; //获取剩余文件的大小
 79      FileSize = FormerOpenStream.Read(buffer, 0, leftSize); //读取剩余的字节
 80      FormerOpenStream.Flush();
 81      ToFileOpenStream.Write(buffer, 0, leftSize); //写入剩余的部分
 82      ToFileOpenStream.Flush();
 83  }
 84  else //如果整体拷贝,即每次拷贝内容大于文件总长度
 85  {
 86      byte[] buffer = new byte[FormerOpenStream.Length];
 87      FormerOpenStream.Read(buffer, 0, (int)FormerOpenStream.Length);
 88      FormerOpenStream.Flush();
 89      ToFileOpenStream.Write(buffer, 0, (int)FormerOpenStream.Length);
 90      ToFileOpenStream.Flush(); 
 91   }
 92  FormerOpenStream.Close();
 93  ToFileOpenStream.Close();
 94  if (MessageBox.Show("copy finished") == DialogResult.OK)
 95  {
 96      progressBar1.Value = 0;
 97      txtOriginalFile.Clear();
 98      txtCopyFile.Clear();
 99      str = ""; 
100   } 
101      } 
102      #endregion 
103    
104      public delegate void CopyFile_Delegate(); //定义委托/托管线程
105     /// <summary>
106     /// 在线程上执行委托(设置托管线程函数)
107     /// </summary>
108     public void SetCopyFile()
109     {
110  //this.Invoke(new CopyFile_Delegate(RunCopyFile)); //对指定的线程进行托管
111  //下面两行代码等同上面一行代码
112  CopyFile_Delegate copyfile_delegate = new CopyFile_Delegate(RunCopyFile); //创建delegate对象
113  this.Invoke(copyfile_delegate); //调用delegate            
114     } 
115  
116     /// <summary>
117     /// 设置线程,运行copy文件,它与代理CopyFile_Delegate应具有相同的参数和返回类型
118     /// </summary>
119     public void RunCopyFile()
120     {
121  CopyFile(txtOriginalFile.Text, txtCopyFile.Text + "\\" + str, 1024, progressBar1); //复制文件
122  Thread.Sleep(0); //避免假死 
123  thdCopyFile.Abort();  //关闭线程 
124      } 
125  
126     private void btnOriginalFile_Click(object sender, EventArgs e)
127     {
128  if (openFileDialog1.ShowDialog() == DialogResult.OK)  //打开文件对话框
129  {
130      txtOriginalFile.Text = openFileDialog1.FileName;  //获取源文件的路径
131  } 
132      } 
133      private void btnCopyFile_Click(object sender, EventArgs e)
134     {
135  if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
136  {
137      txtCopyFile.Text = folderBrowserDialog1.SelectedPath;//获取目的文件的路径               
138  }
139     } 
140  
141     private void btnBeginToCopy_Click(object sender, EventArgs e)
142     {                    
143 
144  if (txtOriginalFile.Text.Trim() == string.Empty)
145  {
146      MessageBox.Show("OriginalFile cannot be empty!");
147      return;
148 
149  }
150  else
151  {
152      str = txtOriginalFile.Text;//记录源文件的路径
153      str = str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1); //获取源文件的名称
154  } 
155  
156  if (txtCopyFile.Text.Trim() == string.Empty)
157  {
158      MessageBox.Show("The Copyfile path cannot be empty!");
159      return;
160  }
161  else
162  {
163      thdCopyFile = new Thread(new ThreadStart(SetCopyFile));
164      thdCopyFile.Start();
165  }             
166 
167     } 
168  
169     /// <summary>
170     /// 给textbox增加tooltip
171     /// </summary>
172     /// <param name="sender"></param>
173     /// <param name="e"></param>
174     private void txtOriginalFile_MouseHover(object sender, EventArgs e)
175     {
176  ToolTip tooltip = new ToolTip();
177  if (txtOriginalFile.Text.Trim() != string.Empty)
178  {
179 
180      tooltip.Show(txtOriginalFile.Text, txtOriginalFile);
181  }
182  else
183  {
184      tooltip.Hide(txtOriginalFile);
185  }
186 
187     }
188     /// <summary>
189     /// 给textbox增加tooltip
190     /// </summary>
191     /// <param name="sender"></param>
192     /// <param name="e"></param>
193     private void txtCopyFile_MouseHover(object sender, EventArgs e)
194     {
195  ToolTip tooltip = new ToolTip();
196  if (txtCopyFile.Text.Trim() != string.Empty)
197  {
198 
199      tooltip.Show(txtCopyFile.Text, txtCopyFile);
200  }
201  else
202  {
203      tooltip.Hide(txtCopyFile); 
204  } 
205      } 
206  }
207 }
208  
复制文件显示进度条

 

posted @ 2015-03-04 23:48  民兵小队长  阅读(2142)  评论(0编辑  收藏  举报