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?
thousandjulysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

esoftbgCommented:
I think the other process could not allow changing the attributes of the file to readonly until finished usage ....
0
thousandjulysAuthor Commented:
no, it can...
0
esoftbgCommented:
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;
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

thousandjulysAuthor Commented:
i know HOW to do that, what my question was is there a more legit, cleaner method? im not looking for code.
0
MadshiCommented:
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).
0
thousandjulysAuthor Commented:
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?
0
MadshiCommented:
Nope, changing the attributes has no effect on a file handle which is already open. I think you're misinterpreting the meaning of the attributes here. If you don't believe me then just try it. WriteFile will not fail if you add the write protected attribute.

There is no specific driver I was talking about. You would need to create your own little driver which calls a specific API with specific flags to open a locked file. And for that you would probably need to use MSVC.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thousandjulysAuthor Commented:
oh, i understand now, thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.