-->

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

2019-03-03 23:28发布

问题:

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;