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

首頁 > 學院 > 開發設計 > 正文

使用C#在進度條中顯示復制文件的進度

2019-11-18 19:06:57
字體:
來源:轉載
供稿:網友

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

}
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 毕节市| 泌阳县| 繁峙县| 陇川县| 德清县| 六盘水市| 阿拉尔市| 永靖县| 嵊州市| 丁青县| 裕民县| 平利县| 盘山县| 璧山县| 达孜县| 庄浪县| 页游| 聂荣县| 方城县| 娄烦县| 绥阳县| 巴塘县| 桦川县| 汽车| 略阳县| 大丰市| 惠水县| 青龙| 临沂市| 驻马店市| 库尔勒市| 中超| 林州市| 昂仁县| 汕尾市| 石景山区| 哈巴河县| 卢氏县| 叙永县| 舞钢市| 镇康县|