Code List:
-------------------------------------------------------------------------
/*****************************************************************
** File Name: frmMain.cs
** Copyright (c) 1999 -2003
** Creator: FirePhoenix
** Created Date: 2004-11-13 15:24
** Modifier:
** Modify Date:
** Description:
** Version:1.0
******************************************************************/
#region Using Directives
using System;
using System.IO ;
using System.Xml ;
using System.Collections ;
using System.Reflection ;
using System.Text ;
using System.Data ;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Threading ;
#endregion
namespace WindowsApplication4
{
/// <summary>
/// Copy Large File
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
#region
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button btnCopy;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Initialize Components
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.btnCopy = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(8, 16);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(208, 16);
this.progressBar1.TabIndex = 0;
//
// btnCopy
//
this.btnCopy.Location = new System.Drawing.Point(8, 48);
this.btnCopy.Name = "btnCopy";
this.btnCopy.TabIndex = 1;
this.btnCopy.Text = "Copy";
this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(232, 77);
this.Controls.Add(this.btnCopy);
this.Controls.Add(this.progressBar1);
this.Name = "frmMain";
this.Text = "Copy File";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Entry Point
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
#endregion
int totalSize; //Total Size
int position; //Position
const int BUFFER_SIZE = 4096;
byte[] buffer;
Stream stream;
private void btnCopy_Click(object sender, System.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:\\copyedFile.bin" ) )
File.Delete( "c:\\copyedFile.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 );
}
else
{
fs.Close();
}
}
/// <summary>
/// Asynchronously copy file
/// </summary>
/// <param name="ar"></param>
private void AsyncCopyFile( IAsyncResult ar )
{
int readedLength ;
// Lock FileStream
lock( stream )
{
readedLength = stream.EndRead( ar ); // When stream endread, get readed length
}
// Write to disk
FileStream fsWriter = new FileStream( "C:\\copyedFile.bin" , FileMode.Append , FileAccess.Write );
fsWriter.Write( buffer , 0 , buffer.Length );
fsWriter.Close();
// Current stream position
position += readedLength;
// Response UI
MethodInvoker m = new MethodInvoker( SynchProgressBar );
m.BeginInvoke( null , null );
if ( position >= totalSize ) // Read over.
{
stream.Close(); //Close FileStream
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.Maximum = totalSize;
this.progressBar1.Value = position ;
}
}
}
分享到:
相关推荐
MFC文件复制 删除,进度条显示文件复制进度,同时显示百分比,用文件对话框选择文件,进度条颜色并改变,代码通俗易懂
网上好多负责文件夹的进度条写的都有问题,在此基础上,自己修改了一下,进度条可以完美显示出来,另外还加了一个label动态显示拷贝的文件名,用的多线程,里面一些细节大家还是要注意一下的。另外拷贝文件夹是是...
弹出模式窗口显示进度条 文件复制进度条 c#进度条 文件进度条 弹窗进度条
C#实现异步复制文件,并且显示进度条,代码简单易懂,效果不错!(注明:VS2010)
摘要:C#源码,文件操作,复制文件,进度条 C#带进度条的复制文件功能演示,对文件进行复制,并在复制完成后关闭线程。如果分段拷贝,即每次拷贝内容小于文件总长度,根据传输的大小,定义一个字节数组,记录传输的大小...
根据文件大小显示文件复制进度条,操作步骤:C#实例化FileStream类,打开文件对话框,获取源文件的路径,获取目的文件的路径,创建一个线程,在线程上执行指定的委托,对文件进行复制,并在复制完成后关闭线程,设置...
C# 文件复制时,如何调用操作系统文件复制进度提示窗口。而不自己写代码,来操作文件复制的进度
与webclient下载功能类似,3个新增的可缺省参数: Prog参数代表progressbar,Value代表lable,Cover代表是否替换同名文件。
http://blog.csdn.net/much0726/archive/2008/12/17/3541278.aspx 实现代码
C#大文件复制,单文件进度条,总进度条,还有速度计算。
实例050 设置货币值中使用的小数位数 64 实例051 格式化输入数据为货币格式 65 实例052 开发一个进制转换器 66 3.4 日期时间格式的数据处理 67 实例053 动态获得系统当前日期和时间 67 实例054 手动设置系统日期时间...
C# Winform版 仿Win8进度条的源码,你看下效果截图就明白了,和Xp的进度条似乎风格大不一样了,进度条在变化的过程中,适时显示当前的数字进度、复制文件速率、剩余时间和剩余项目多少等,以更详细的信息展示程序...
实例222 复制文件时显示复制进度 310 实例223 批量复制文件 312 6.6 指定类型的文件操作 313 实例224 文本文件的操作 313 实例225 使用ROT13加密解密文件 314 6.7 其他 315 实例226 获取窗口文本 315 实例227...
实例222 复制文件时显示复制进度 310 实例223 批量复制文件 312 6.6 指定类型的文件操作 313 实例224 文本文件的操作 313 实例225 使用ROT13加密解密文件 314 6.7 其他 315 实例226 获取窗口文本 315 实例227...
18.2.4 如何在文件夹中遍历文件 463 18.2.5 如何实现播放进度条 464 18.3 设计过程 465 18.3.1 磁性窗体的设置 465 18.3.2 播放窗体的设计 472 18.3.3 列表窗体的设计 482 18.3.4 歌词窗体的设计 485 ...