国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

使用C#在進(jìn)度條中顯示復(fù)制文件的進(jìn)度

2024-07-21 02:18:17
字體:
供稿:網(wǎng)友

最大的網(wǎng)站源碼資源下載站,

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>
/// 必需的設(shè)計器變量。
/// </summary>
private system.componentmodel.container components = null;

public frmmain()
{
//
// windows 窗體設(shè)計器支持所必需的
//
initializecomponent();

//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}

#region initialize components
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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 ;
}

}
}



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大同市| 商河县| 青川县| 渑池县| 安阳县| 浙江省| 花垣县| 南皮县| 金乡县| 新乡县| 巴彦县| 吴堡县| 伊吾县| 梨树县| 临沭县| 杂多县| 江城| 华容县| 长岛县| 龙岩市| 龙海市| 庆云县| 成都市| 丰宁| 安宁市| 秦皇岛市| 广德县| 石棉县| 赤水市| 甘肃省| 南投市| 龙泉市| 潜江市| 云和县| 福海县| 万山特区| 民县| 光山县| 东莞市| 江源县| 资溪县|