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?
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?
I think the other process could not allow changing the attributes of the file to readonly until finished usage ....
ASKER
no, it can...
procedure Make_File_Read_Only(FileNa me: string);
begin
if not ((FileGetAttr(FileName) and faReadOnly) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);
end;
procedure Make_File_Not_Read_Only(Fi leName: 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:\Re ad.txt');
end;
procedure TForm1.Button2Click(Sender : TObject);
begin
Make_File_Not_Read_Only('C :\Read.txt ');
end;
begin
if not ((FileGetAttr(FileName) and faReadOnly) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);
end;
procedure Make_File_Not_Read_Only(Fi
begin
if ((FileGetAttr(FileName) and faReadOnly) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);
end;
procedure TForm1.Button1Click(Sender
begin
Make_File_Read_Only('C:\Re
end;
procedure TForm1.Button2Click(Sender
begin
Make_File_Not_Read_Only('C
end;
ASKER
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).
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).
ASKER
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?
anyways, thanks for your reply. what are you drivers that are able to open locked files?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oh, i understand now, thanks!