C#在进度条中显示复制文件的进度
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Xml;
- using System.Collections;
- using System.Reflection;
- using System.Threading;
-
- namespace CopyForm
- {
- public partial class frmMain : Form
- {
- int totalSize;
- int position;
- const int BUFFER_SIZE = 4096;
- byte buffer;
- Stream stream;
-
- public frmMain()
- {
- InitializeComponent();
- }
-
- private void frmMain_Load(object sender, EventArgs e)
- {
- }
-
- private void btnCopy_Click(object sender, EventArgs e)
- {
- string strFile = "";
- OpenFileDialog dlg = new OpenFileDialog();
- if (dlg.ShowDialog() == DialogResult.OK)
- {
- strFile = dlg.FileName;
- }
- else
- {
- return;
- }
-
- FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read);
- totalSize = (int)fs.Length;
- stream = fs;
-
-
- if (File.Exists("c:\\CopyFile.bin"))
- {
- File.Delete("c:\\CopyFile.bin");
- }
-
-
- if (totalSize > BUFFER_SIZE)
- {
- buffer = new byte[BUFFER_SIZE];
-
-
- stream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null);
- MessageBox.Show("文件拷贝完毕!");
- }
- else
- {
- fs.Close();
- }
- }
-
-
-
-
-
- private void AsyncCopyFile(IAsyncResult ar)
- {
- int readenLength;
-
-
- lock (stream)
- {
-
- readenLength = stream.EndRead(ar);
- }
-
-
- FileStream fsWriter = new FileStream("c:\\CopyFile.bin", FileMode.Append, FileAccess.Write);
- fsWriter.Write(buffer, 0, buffer.Length);
- fsWriter.Close();
-
-
- position += readenLength;
-
-
- MethodInvoker m = new MethodInvoker(SynchProgressBar);
- m.BeginInvoke(null, null);
-
- if (position >= totalSize)
- {
- stream.Close();
- return;
- }
-
-
- lock (stream)
- {
- int leftSize = totalSize - position;
-
- if (leftSize < BUFFER_SIZE)
- {
- buffer = new byte[leftSize];
- }
- stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncCopyFile), null);
- }
- }
-
- private void SynchProgressBar()
- {
- this.progressBar1.Minimum = 0;
-
-
- SetControlPropertyValue(this.progressBar1, "Maximum", totalSize);
- SetControlPropertyValue(this.progressBar1, "Value", position);
- }
-
-
-
-
-
-
-
-
- delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
- private void SetControlPropertyValue(Control oControl, string propName, object propValue)
- {
- if (oControl.InvokeRequired)
- {
- SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
- oControl.Invoke(d, new object { oControl, propName, propValue });
- }
- else
- {
- Type t = oControl.GetType();
- PropertyInfo props = t.GetProperties();
- foreach (PropertyInfo p in props)
- {
- if (p.Name.ToUpper() == propName.ToUpper())
- {
- p.SetValue(oControl, propValue, null);
- }
- }
- }
- }
-
- private void btnExit_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
-
- private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
- {
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- }