How to Read a File Which Is in Use

How do you read a file which is in use?

using (FileStream stream = File.Open("path to file", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{

}
}
}

The FileAccess specifies what YOU want to do with the file.
The FileShare specifies what OTHERS can do with the file while you have it in use.

In the example above, you can open a file for reading while other processes can have the file open for read/write access. In most cases, this will work for opening logfiles that are in use.

How can I read a file even when getting an in use by another process exception?

FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);

while (!logFileReader.EndOfStream)
{
string line = logFileReader.ReadLine();
// Your code here
}

// Clean up
logFileReader.Close();
logFileStream.Close();

Original source for code

Reading a file used by another process

If notepad can read the file then so can you, clearly the program didn't put a read lock on the file. The problem you're running into is that StreamReader will open the file with FileShare.Read. Which denies write access. That can't work, the other program already gained write access.

You'll need to create the StreamReader like this:

using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(fs, Encoding.Default)) {
// read the stream
//...
}

Guessing at the Encoding here. You have to be careful with this kind of code, the other program is actively writing to the file. You won't get a very reliable end-of-file indication, getting a partial last line is quite possible. In particular troublesome when you keep reading the file to try to get whatever the program appended.

Can I simply 'read' a file that is in use?

You can read the file only if the program that opened the file first specified read sharing rights on that file.

If the file does indeed have no read sharing rights though, you wouldn't be able to copy it in the first place.

You may not be able to access a file if you are specifying a sharing right that conflicts with the sharing right of a program that already has the file opened. For example you can't grant write access if the program that already has it opened isn't granting write access.

If the program that opened the file in the first place supports Volume Shadow Copy (VSS), you can also use VSS to gain access to the file.

There are commercial software drivers that allow you to access such files, even when they are in use. You used to be able to get Open File Manager by St-Bernards, and you can also use File Access Manager (FAM) by VisionWorks Solutions Inc. These drivers are typically OEM'ed to backup software companies for inclusion in their products.

VSS works by telling the program that has the file opened already that another program would like to read from the file. VSS then does a copy of the file and lets you read from this copy. VSS does not work for legacy applications.

FAM transparently works for legacy and non-legacy programs alike by specifying an 'allowed list' of applications that can access exclusively opened and locked files. Only programs in this list are allowed access to these files. When a file is being opened, it goes into cache mode so that you will obtain a copy of the file as it was when the 'backup/open' of the file started. At this point the program that originally opened the file sees the file as it actually is, and the second program in the allowed list, sees the file as it was when the 'open/backup' of the file happened. This ensures consistency of the file.

How to read a file that is in use by other process?

Use fmOpenWrite or fmShareDenyNone to enable sharing.

Read a file while some other program is writing the file in python

What you are trying to do should be fairly easy. I am pretty sure your code will technically work, but you really should handle files using a context manager. I also restructured your code to do what I think you intended to do a little better.

File that reads

    import os
import time

we_have_written_bye = False

while we_have_written_bye = False:
with open("file.py", "r") as f
x = f.read()
if x.find("bye")!=-1:
we_have_written_bye = True

# file closes automatically with a context manager so
# this is removed. Note, if bye was not written yet, we
# close the file, then wait for a second by sleeping below

time.sleep(1)


File that writes

  import os

with open("file.py", "w", os.O_NONBLOCK) as f
f.write("bye")
f.flush() # not sure why you want to flush the file contents here
f.close()

file.py

  hello

The two programs should work seamlessly. This is because the file object can not be opened if another program is writing to it. You may run into an issue with this, but if the writes are small I believe the standard library will wait long enough for the file lock to be given up.

For a tutorial on context managers please see:

https://www.youtube.com/watch?v=Lv1treHIckI

this is part of a great series of semi-advanced python tutorials that will certainly up your game. Helped me tonnes



Related Topics



Leave a reply



Submit