1 頁 (共 1 頁)

如何copy正在使用中的檔案

發表於 : 2008年 11月 27日, 18:28
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;