如何copy正在使用中的檔案

有關Delphi 的語法, 程式, 等
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

如何copy正在使用中的檔案

文章 tim »

當檔案被鎖定時是無法複製的, 如資料庫的 data檔, 可以使用下面的方式來進行 copy, 配合 TFileStream 與 CopyFrom 函數, 可以達到複製 open file 的功能!!

代碼: 選擇全部

     
    function CopyFileTo(const Source, Destination: string): Boolean; 
    var 
      SourceStream: TFileStream; 
    begin 
      result := false; 
      if not FileExists(Destination) then 
      begin 
        SourceStream := TFileStream.Create(Source, fmOpenRead); 
        try 
          with TFileStream.Create(Destination, fmCreate) do 
          try 
            CopyFrom(SourceStream, 0); 
          finally free; 
          end; 
        finally SourceStream.free; 
        end; 
        result := true; 
      end; 
    end; 
多多留言, 整理文章, 把經驗累積下來.....
回覆文章