How do I open a file that is in use in C#?

I had a situation recently in which I needed to display in my application the log file that was being generated by the application which was using log4net. Any time I tried to open the log file I would get an exception because the file was in use. I searched google for the answer as I normally do and found many others trying to find the same exact answer. Some came up with ideas like copying the original file to another temporary file and opening it. Others seemed to reach a dead end. However I soon figured out what my issue was.

When reading my file using StreamReader it appears that while I assumed since I was reading the file it should not be an issue if it was in use since I was only reading it. Well that is were I was making my wrong assumption. It appears that the StreamReader object opens the file and tries to set an exclusive lock on the file while reading the contents. Since in my case another program already has an exclusive lock it would fail.

So I started to look at how I could open the file in truly a read only mode. While I found people talking again about opening a file in use for readonly access it was for some reason not clear to me what the exact solution was.

So what is the solution? System.IO.FileShare

By creating a FileStream object and setting FileShare.ReadWrite I am saying that I want to open the file in a mode that allows other files to Read and Write to/from the file while I have it opened.

Here is an example code snipet of how I read my log file that was currently in use by the logging object log4net. Hope this helps someone else.


private void LoadFile()
{
    try
    {
        using(FileStream fileStream = new FileStream(
            "logs/myapp.log",
            FileMode.Open,
            FileAccess.Read,
            FileShare.ReadWrite))
        {
            using(StreamReader streamReader = new StreamReader(fileStream))
            {
                this.textBoxLogs.Text = streamReader.ReadToEnd();
            }
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show("Error loading log file: " + ex.Message);
    }
}


kick it on DotNetKicks.com

Comments (10) -

gazik
4/24/2009 6:49:21 AM #

Thank you very much!!!!!!
Excellent explanation!!!

MeBo
5/1/2009 5:26:42 PM #

Worked like a charm. Thanx

Alex Goss
5/20/2009 7:16:00 AM #

I have had this issue for days and have had to resort to some of the hacks - this is just a so much better solution. More importantly I've learnt something.
Very many thanks.

Matthew
10/1/2009 8:02:22 AM #

It worked!  Hooray!
Praise god.  God in this case being the writer of this article.
Thank you so much.

viccer
10/20/2009 9:56:45 PM #

Only works with files being used by a .net app. Not working on access temp files produced by flash player.

Nitin Sawant
12/7/2009 1:53:57 AM #

Dear,

Thanks for sharing

Jenny
12/14/2009 9:04:13 AM #

Great explanation! Thanks a lot!

Helmut
8/26/2010 1:07:51 AM #

Thanks for sharing this! Works great.

Pag
8/31/2010 6:27:32 AM #

Works like a charm!  Great post!  Thanks for sharing!

Niklas
4/4/2011 6:58:43 AM #

Thanks! I made the same false assumption on StreamReader, pulled my hair out in failed workarounds till I found this. Great relief, thanks again!

Pingbacks and trackbacks (3)+

Add comment

RecentComments

Comment RSS