Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading a file that is currently in use.

Posted on 2004-10-22
8
Medium Priority
?
167 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:thousandjulys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12387261
I think the other process could not allow changing the attributes of the file to readonly until finished usage ....
0
 

Author Comment

by:thousandjulys
ID: 12387398
no, it can...
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12389802
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:thousandjulys
ID: 12390813
i know HOW to do that, what my question was is there a more legit, cleaner method? im not looking for code.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 12391947
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
 

Author Comment

by:thousandjulys
ID: 12392009
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
 
LVL 20

Accepted Solution

by:
Madshi earned 500 total points
ID: 12392101
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
 

Author Comment

by:thousandjulys
ID: 12395176
oh, i understand now, thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question