How to copy a locked file like .pst using delphi xe3

徘徊边缘 提交于 2019-12-20 06:27:02

问题


I am struggling to find an answer to the following problem. Any and all help would be appreciated.

I am using the following code to try and copy an outlook.pst file while outlook is open. And i cannot get it to succeed. It does not give an error, it just doesnt copy the file.

copyfile('C:\Users\Administrator\Documents\Outlook Files\Outlook.pst','F:\Outlook.pst');

If you guys know how i will be able to copy a locked file like that please assist.

I have tried and found that TFilestream also does not work.

And those 2 are the only options i know off. any help would be greatly appreciated.

Thank You

I have tried the following code as-well and get an error saying that the file is in use from another process(outlook).

procedure TForm1.Button2Click(Sender: TObject);
var
   NewFileName: string;
   NewFile: TFileStream;
   OldFile: TFileStream;
Begin
           NewFileName:='F:\outlook.pst';
           OldFile := TFileStream.Create('C:\Users\Administrator\Documents\Outlook Files\outlook.pst', fmOpenRead or fmShareDenyWrite);
            try
              NewFile := TFileStream.Create(NewFileName, fmCreate or fmShareDenyNone);
              try
                NewFile.CopyFrom(OldFile, OldFile.Size);
              finally
                FreeAndNil(NewFile);
              end;
            finally
              FreeAndNil(OldFile);
            end;
end;

Please see the following link. If anybody can convert the code. the problem should be solved. How to copy a pst file while it is open using c#


回答1:


PST provider locks PST files until the parent process terminates. Even if you close the PST file from Outlook, it will be kept open for 30 minutes for the performance reasons.

Do you programmatically open the PST file in Outlook?




回答2:


Try the fmShareDenyNone flag when creating the TFileStream object:

stream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone);
try 
   slFile.LoadFromStream(stream);
finally
   stream.Free;
end;

Function to read the date from a file:

function GetFileDate(TheFileName: string): string;
var
  FHandle: integer;
begin
  FHandle := FileOpen(TheFileName, fmShareDenyNone);
  try
    Result := DateTimeToStr(FileDateToDateTime(FileGetDate(FHandle)));
  finally
    FileClose(FHandle);
  end;
end;


来源:https://stackoverflow.com/questions/21556184/how-to-copy-a-locked-file-like-pst-using-delphi-xe3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!