How do you read a file which is in use? [duplicate]

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.

Leave a Comment