[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Reading a file that is currently in use.

Posted on 2004-10-22
8
Medium Priority
?
168 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
  • 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
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

590 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