Skip to content

How do i read a file open in another program?

Athian Miere

Asked Dec 17, 2005, 3:25 AM EST1 answer
Share
LinkedInX
I'm trying to read from a *txt file that a program uses as a log.  I haven't found any good articles on reading from logs using C#. so i created a streamreader object and i have it read from the log.txt

well, i want to read from the log while it's being updated by another program, so i have a file systemwatcher that triggers a change event, and that event tells the streamreader to go look at the file.  but I keep getting an error from the streamreader that it can't access the file since it is open by another program. 

what am i doing wrong? is the streamreader the wrong object to use?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Ok, think I figured it out myself

    using ( StreamReader reader = new StreamReader(new FileStream(fileName,
                         FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) )
    0