Link to home
Start Free TrialLog in
Avatar of thousandjulys
thousandjulys

asked on

Reading a file that is currently in use.

What is the best solution to read the contents of a file that is currently being written or in use by another process, and the other process doesnt now allow share read for the file.

would it be a good solution to change the attributes to the file to readonly so the other process closes the file handle, giving you access to it, or is there a more legit, cleaner method?
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

I think the other process could not allow changing the attributes of the file to readonly until finished usage ....
Avatar of thousandjulys
thousandjulys

ASKER

no, it can...
procedure Make_File_Read_Only(FileName: string);
begin
  if not ((FileGetAttr(FileName) and faReadOnly) > 0) then
    FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);
end;

procedure Make_File_Not_Read_Only(FileName: string);
begin
  if ((FileGetAttr(FileName) and faReadOnly) > 0) then
    FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Make_File_Read_Only('C:\Read.txt');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Make_File_Not_Read_Only('C:\Read.txt');
end;
i know HOW to do that, what my question was is there a more legit, cleaner method? im not looking for code.
I don't think that changing the attribs has any effect in this situation.

There are 2 ways to solve the problem, I think:

(1) The other process must have a handle to the file open. You could try to find the handle value and call DuplicateHandle. This way you should get access to the file content.

(2) In XP/2003 drivers are able to open locked files (by using a special flag).
changing the attributes causes the writing process to error out on the writing function, therefore closing its handle (apparently).

anyways, thanks for your reply. what are you drivers that are able to open locked files?
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh, i understand now, thanks!