Results 1 to 4 of 4

Thread: How to open a file in use?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Question 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...
    Last edited by HongKongCV; Nov 2nd, 2011 at 02:13 PM.

  2. #2
    Hyperactive Member Max Peck's Avatar
    Join Date
    Oct 2007
    Posts
    384

    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
    The name's "Peck" .... "Max Peck"

    "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." - Red Adair

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    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?

  4. #4
    Hyperactive Member Max Peck's Avatar
    Join Date
    Oct 2007
    Posts
    384

    Re: How to open a file in use?

    Quote Originally Posted by HongKongCV View Post
    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
    The name's "Peck" .... "Max Peck"

    "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." - Red Adair

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width