首页 > 开发 > 综合 > 正文

使用C#在进度条中显示复制文件的进度

2024-07-21 02:18:17
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

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 ;
}

}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表