Use C # to display the progress of the replication file in the progress bar

xiaoxiao2021-03-06  73

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 Directivesusing 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; 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; /// /// The required designer variable. /// private system.componentmodel.container components = null;

Public frmmain () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose (4);

#Region Initialize Components ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// 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 = 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; this. .Text = "copy file"; this.ResumeLayout (false);} #ENDREGION

///

/// entry point /// [stathread] static void main () {Application.Run (new frmmain ());}

#ndregion

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 heich 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 (asyncopyfile), null;} else {fs.close ();}} ///

/// asynchronously copy file /// < / summary> /// 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

Stream.Beginread (Buffer, 0, Buffer.Length, New AsyncCallback (asynccopy), null;}}

Private void synchprogressbar () {this.progressbar1.maximum = Totalsize; this.progressbar1.value = position;}}}

转载请注明原文地址:https://www.9cbs.com/read-91958.html

New Post(0)