如何在Delphi 5下拷贝一个文件

可以使用API函数CopyFile。如:
     CopyFile('c:\autoexec.bat', 'd:\my.bat', false);
    

    如果要拷贝多个文件或整个目录,可以使用SHFileOperation。例子:
    uses ShellAPI;
    
     procedure TForm1.Button1Click(Sender: TObject) ;
     var
     Fos : TSHFileOpStruct;
     Buf : array[0..4096] of char;
     p : pchar;
     sDest : string;
     begin
     FillChar(Buf, sizeof(Buf), #0) ;
     p := @buf;
     p := StrECopy(p, 'C:\FirstFile.ext1') + 1;
     p := StrECopy(p, 'C:\SecondFile.ext2') + 1;
     StrECopy(p, 'C:\ThirdFile.ext3') ;
    
     sDest := 'e:\';
    
     FillChar(Fos, sizeof(Fos), #0) ;
     with Fos do begin
     Wnd := Handle;
     wFunc := FO_COPY;
     pFrom := @Buf;
     pTo := sDest;
     fFlags := 0;
    end;
     if ((SHFileOperation(Fos) <> 0) or
     (Fos.fAnyOperationsAborted <> false)) then
     ShowMessage('操作被用户取消了')
     end;
    

    例子中的文件名可以使用通配符。

posted on 2005-06-13 09:39  老弹  阅读(717)  评论(0编辑  收藏  举报

导航