Solved

Reading a file that is currently in use.

Posted on 2004-10-22
166 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
Question by:thousandjulys
    8 Comments
     
    LVL 12

    Expert Comment

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

    Author Comment

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

    Expert Comment

    by:esoftbg
    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
     

    Author Comment

    by:thousandjulys
    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
    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
    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:
    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
    oh, i understand now, thanks!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    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…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now