How to open a file in use?
Okay, I have a program that processes a lot of data, running for 4-5 hours and creating a log file. I want to write a second program to scan the log file while it is being created. I know this should be possible, because I can double-click on the log file (in windows explorer) while the program is running and view the log file using notepad. When I try to open the file via code, however, it tells me the file is in use.
The process cannot access the file '[filename]' because it is being used by another process.
The log file is opened by the application, the stream is flushed after each line is written, and the file is closed when the application terminates.
I tried opening it using a StreamReader from the second application, and it won't let me open it. Any ideas how I can get around this?
Since I can open the logfile as it is being created with Notepad, it should be possible. All I want to do is read from it, not write to it...
Re: How to open a file in use?
Generally speaking you should be able to open an in-use file in READ-ONLY mode. See if there is an attribute setting to the StreamReader object that will enable you to open it in that mode. The O/S really ought to allow you to do this.
-Max
Re: How to open a file in use?
Hmmm... I thought that was inherent in the fact that I was using a StreamReader to open the file. Sort of implied, you know?
Re: How to open a file in use?
Quote:
Originally Posted by
HongKongCV
Hmmm... I thought that was inherent in the fact that I was using a StreamReader to open the file. Sort of implied, you know?
I haven't done a lot of regular file I/O lately - mostly SQL Server. When opening the StreamReader on the file, are you able to specify the mode that the file is opened in? I'll bet that, somewhere, you can. It may default to opening the file with a locking attribute (exclusive). In that case you will have an issue if the file is in use. You may have to explicitly tell the framework that you want to open it in shared mode.
-Max