C#在进度条中显示复制文件的进度
来源:互联网 发布:先知数据科技有限公司 编辑:程序博客网 时间:2024/04/20 21:58
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;
//Delete file which aready exists.
if (File.Exists("c://CopyFile.bin"))
{
File.Delete("c://CopyFile.bin");
}
//Copy file while larger than 4KB.
if (totalSize > BUFFER_SIZE)
{
buffer = new byte[BUFFER_SIZE];
// Async Invoke
stream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null);
MessageBox.Show("文件拷贝完毕!");
}
else
{
fs.Close();
}
}
/// <summary>
/// Asynchronously copy file
/// </summary>
/// <param name="ar"></param>
private void AsyncCopyFile(IAsyncResult ar)
{
int readenLength;
//Lock FileStream
lock (stream)
{
// When stream endread, get readed length
readenLength = stream.EndRead(ar);
}
// Write to disk
FileStream fsWriter = new FileStream("c://CopyFile.bin", FileMode.Append, FileAccess.Write);
fsWriter.Write(buffer, 0, buffer.Length);
fsWriter.Close();
// Current stream position
position += readenLength;
// Response UI
MethodInvoker m = new MethodInvoker(SynchProgressBar);
m.BeginInvoke(null, null);
if (position >= totalSize) //read over
{
stream.Close();
return;
}
// Continue to read and write
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;
//this.progressBar1.Maximum = totalSize;
//this.progressBar1.Value = position;
SetControlPropertyValue(this.progressBar1, "Maximum", totalSize);
SetControlPropertyValue(this.progressBar1, "Value", position);
}
/// <summary>
/// Cross-thread operation not valid: (solution key)
/// Control 'progressBar1' accessed from a thread other than the thread it was created on
/// </summary>
/// <param name="oControl"></param>
/// <param name="propName"></param>
/// <param name="propValue"></param>
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();
}
}
}
【本文参考于网络,并加以修改,编译通过】
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- [懵懂边缘]使用C#在进度条中显示复制文件的进度
- C# 文件复制 进度条显示
- VS2010-MFC下在状态栏中添加进度条用于显示当前文件的读取进度
- 在winform中使用进度条显示任务的执行进度
- 在winform中使用进度条显示任务的执行进度
- 复制文件显示进度
- VC实现显示文件复制的进度
- 《世界因你不同》——去微软,回中国
- 网络爬虫-Heritrix 和 Nutch比较与分析
- 注册表的文件类型关联[转]
- XML 显示双引号
- 人民币利率水平(2010.10.20之前)
- C#在进度条中显示复制文件的进度
- c# 支持XML序列化的泛型 Dictionary
- 简单HTML 字体,颜色表示(转载)
- Object Serialization in C#
- test
- c# Web 前端与 Server端通信的方法:ChannelServices
- 学习卡尔曼滤波
- 嵌入式系统开发之中断控制的实现
- c# 中 List能对T对象排序的方法