When copying a file, how to display the progress bar (use the Blockread function to read the data and insert the application. ProcessMessages)

Source: Internet
Author: User
Tags getstream

procedureMycopyfile (SOURCEF,TARGETF:string; i:integer);varFromf,tof:file;       Numread,numwritten:integer; Buf:Array[1..2048] ofChar; N:integer;beginAssignFile (FROMF,SOURCEF); Reset (FROMF,1);{Record size = 1}AssignFile (TOF,TARGETF);{Open Output File}Rewrite (ToF,1);{Record size = 1}N:=0; RepeatBlockread (FROMF, Buf, SizeOf (Buf), numread); Form1. Progressbar1.position:=sizeof (BUF) *n* -    DivFileSize (FROMF) *iDiv 4; Application.                   ProcessMessages; //Show ProgressBlockwrite (ToF, Buf, Numread, Numwritten);           Inc (N); until(Numread =0)or(Numwritten <>numread); Form1. Progressbar1.position:= -;           CloseFile (FROMF); CloseFile (ToF);End;

Full code:

The following example is a key code for an example of a Delphi copy file, plus a progress bar method:procedureTform1.button1click (sender:tobject);ConstGetPath='C:/temp/get.rar'; SetPath='C:/temp/set.rar';varGetstream,setstream:tfilestream;  Num, N:integer;  Buf:pbyte; Bufsize,block:integer;begin  if  notFileExists (GetPath) Then  beginShowMessage ('source file does not exist');  Exit; End; GetStream:= TFileStream.Create(GetPath, Fmopenreadorfmshareexclusive); SetStream:= TFileStream.Create(SetPath, fmcreate); Num:=getstream.size; Setstream.size:=num; Getstream.position:=0; Setstream.position:=0; BufSize:=num; Block:= BufSizeDiv  -;  Getmem (buf, BufSize); Progressbar1.max:= -; Progressbar1.min:=0; Progressbar1.position:=0;  whileNum <>0  Do  beginapplication.processmessages; N:=Block; ifn > num ThenN: =num;    Getstream.readbuffer (buf^, N);    Setstream.writebuffer (buf^, N); Progressbar1.position:= Trunc ((1-num/bufsize) * -);  DEC (num, n); End;  Freemem (buf, BufSize);  Getstream.free;  Setstream.free; ShowMessage ('Copy Complete'); Progressbar1.position:=0;End;End.

http://blog.csdn.net/shuaihj/article/details/6129551

When copying a file, how to display the progress bar (use the Blockread function to read the data and insert the application. ProcessMessages)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.