如何在用Api拷贝文件时,用进度条与之同步?(50分)

  • 主题发起人 主题发起人 ddolphin
  • 开始时间 开始时间
<br>用API恐怕不行,可以用stream拷贝
 
那么你得按照某大小的缓冲来将文件都读出来,不然你怎么知道你读了多少、还有多少呢?
 
你可以将要COPY的文件大小读出,然后用一个定时器控制,每一时刻读取当前COPY目的地文件<br>大小,然后在TIMER事件里控制进度条。当然这是最最原始方法。但是符合你说的用API实现
 
用文件流吧!
 
能给一个stream 拷贝的例子吗?
 
用SHFileOperxxxxx API,其中xxxxx是什么我忘了,xixi,自己查帮助吧
 
// Copy a file using a TFileStream object<br><br>procedure FileCopy( const Source, Dest : string );<br>var<br>&nbsp; S, D: TFileStream;<br>begin<br>&nbsp; S := TFileStream.Create( Source, fmOpenRead );<br>&nbsp; try<br>&nbsp; &nbsp; D := TFileStream.Create( Dest,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fmOpenWrite or fmCreate );<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; D.CopyFrom( S, S.Size );<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; D.Free;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; S.Free;<br>&nbsp; end;<br>end;<br><br><br>// Copy a file using LZCopy <br>// LZCopy is implemented in the LZExpand.dll that ships with Windows. &nbsp;The interfaces to the functions are in the LZExpand unit.<br><br>procedure CopyFile( Source, Dest : string );<br>var<br>&nbsp; S, D : File;<br>begin<br>&nbsp; AssignFile( S, Source );<br>&nbsp; AssignFile( D, Dest ); <br>&nbsp; Reset( S );<br>&nbsp; try<br>&nbsp; &nbsp; Rewrite( D ); <br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; if LZCopy( TFileRec( S ).Handle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TFileRec( D ).Handle ) &lt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise EInOutError.Create('Error using LZCopy');<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; CloseFile( D );<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; CloseFile( S );<br>&nbsp; end;<br>end;<br><br><br>// 自己再把有关进度的部分加进去<br>
 
用SHFileOperation<br><br>
 
使用系统的Copy文件框 <br>use shlobj;<br>function ShellFileCopy (AHandle:HWND;AFrom:string;ATo:String):boolean;<br>var shFileOpRec:TSHFileOpStruct ;<br>begin<br>&nbsp; &nbsp; &nbsp;result := true ; &nbsp;// ah optimism<br>&nbsp; &nbsp; &nbsp;if ( AFrom = '' ) or ( ATo = '' ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result := false ;<br>&nbsp; &nbsp; &nbsp; &nbsp; end ;<br>&nbsp; &nbsp; &nbsp;with shFileOpRec do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Wnd &nbsp; &nbsp;:= AHandle ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the window which owns this dialog<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wFunc &nbsp;:= FO_COPY ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// what kind of operation<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pFrom &nbsp;:= PAnsiChar(AFrom) ; &nbsp; &nbsp; // from<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pTo &nbsp; &nbsp;:= PAnsiChar(ATo) ; &nbsp; &nbsp; &nbsp; // to<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fFlags := &nbsp;FOF_FILESONLY and<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FOF_ALLOWUNDO and<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FOF_SIMPLEPROGRESS;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hNameMappings &nbsp; &nbsp; &nbsp; &nbsp; := nil; // &nbsp;Pointer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // only used if FOF_SIMPLEPROGRESS<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpszProgressTitle &nbsp; &nbsp; := nil ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check if file operation was aborted or not.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( fAnyOperationsAborted = true ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result := false ;<br>&nbsp; &nbsp; &nbsp; end; // with<br>&nbsp; &nbsp; &nbsp;// run the actual operation now<br>&nbsp; &nbsp; &nbsp;if (SHFileOperation (shFileOpRec) = 0 ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result := false ;<br>end;<br><br>
 
简单的:<br>var<br>fromstream,tostream:tfilestream;<br>bytetocopy:longint;<br>fromstream:=tfilestream.create(file1name,fmopenread);<br>tostream:=tfilestreame.create(file2name,fmcreate or fmopenwrite);<br>progressbar.step:=fromstream.size div 1000;<br>bytetocopy:=progressbar.step;<br>while tostream.position+bytetocopy&lt;=fromstream.size do<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;tostream.copyfrom(fromstream,bytetocopy);<br>&nbsp; &nbsp; &nbsp; &nbsp;progressbar.stepit;<br>&nbsp; &nbsp;end;<br>tostream.copyfrom(fromstream,fromstream.size-tostream.size);<br>progressbar.position:=tostream.size;
 
多人接受答案了。
 
后退
顶部