All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Call,to,undefined,function,curl_init()_php资料_编程技术
对.NET,Framework,反射的反思_.net资料_编程技术
C#,怎样实现远程连接SQL,Server2005_.net资料_编程技术
Access升迁SQL,Server_[SQL,Server教程]
ASP.NET实例:增强,GridView,控件的功能(二)_[Asp.Net教程]
在网页中插入媒体播放器详细参考
安装Sql,server,2005中AdventureWork_[SQL,Server教程]
Asp.Net、Ado.net,数据库编程超级攻略_[Asp.Net教程]
asp.net2.0加密数据库连接字符串技巧_[Asp.Net教程]
url传递中文的方法_[Asp.Net教程]
Reading number is top 10 pictures
yy365网站上的美女1
Chinese paper-cut grilles art appreciation7
人造器官和铁肺人
The mother was a stay-at-home children too tired took three baby suicide
Startling Russian girl blind date scene3
YangYuYing and ZhengShaoQiu dance on the generous come interest dye-in-the-wood
Terrorist smile the largest human history an explosion1
29 the belle stars after bath figure5
Most cow mistress ZhaoGongXia face exposure
囚犯暴乱了咋办?
Download software ranking
天龙八部十二宫服务端
Unix video tutorial15
Boxer's Top ten classic battle4
虚拟机汉化软件
Tram sex maniac 2 (H) rar bag2
功夫熊猫2(上集)
VeryCD电驴(EasyMule) V1.1.9 Build09081
尖东毒玫瑰B
Boxer's Top ten classic battle1
Boxer's Top ten classic battle9
delv published in(发表于) 2014/1/24 9:10:54 Edit(编辑)
使用C#在进度条中显示复制文件的进度_[Asp.Net教程]

使用C#在进度条中显示复制文件的进度_[Asp.Net教程]

使用C#在进度条中显示复制文件的进度_[Asp.Net教程]

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
{
///
/// Copy Large File
///

public class frmMain : System.Windows.Forms.Form
{
#region
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button btnCopy;
///
/// 必需的设计器变量。
///

private System.ComponentModel.Container components = null;

public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TOD 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///
/// 清理所有正在使用的资源。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Initialize Components
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

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

///
/// Entry Point
///

[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();
}

}

///
/// Asynchronously copy file
///

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

}
}


来源:http://blog.csdn.net/21aspnet






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.