How To User StreamReader to Open A File ReadOnly in C# (.NET)

 

This is probably something pretty simple, hardly worth blogging about, but it took me a little while (15 minutes)  to figure out how to open a file ReadOnly with StreamReader,  so hopefully, next person looking will hit my blog post right away and save themselves 14 minutes.  StreamReader can be very useful in C#.

Say you have a block of code like the following for simply reading all lines of a file:

StreamReader streamReader = new StreamReader("myfile.txt")
string line;
  while ((line = streamReader.ReadLine()) != null)
  {  
       // do something      
   }

When you run this, you will quickly find out that the file gets opened read/write.

To open the file ReadOnly, you have to add a little bit of extra information to the StreamReader constructor call.

That is:

StreamReader streamReader = new StreamReader(File.OpenRead(file));

Hope this helps!

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he's a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. Rajesh, our answer works perfect!!

  2. Bob,

    I managed to get my requirement coded – which was to read from a file that was continuously getting updated by other process. I think you are looking for similar one… Here’s what I did.

    FileStream fs = new FileStream(“myfile.txt”, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    StreamReader streamReader = new StreamReader(fs);
    string line;
    while ((line = streamReader.ReadLine()) != null)
    {
    // do something
    }

    The FileShare.ReadWrite in fs object is important to let your program know that the file is being updated by another process. This way it reads the new contents that are written while it was already opened for reading. Hope this helps!

    Cheers.

  3. The code looks good to me, but When I manually opened a file and edited it but didn’t save it yet!, Then ran my program with the above code to open it in ReadOnly mode and read the stream. While it is reading the stream, I tried to save the file which I manually updated earlier. This doesn’t allow me to save it saying there is a Sharing violation and cannot save the file. Any ideas why this issue arises even when the file is being opened in File.OpenRead(file) method.

  4. hmm. It looks to me like OpenRead is there, all the way back to .net 2.0 http://msdn.microsoft.com/en-US/library/system.io.file_methods(v=vs.80).aspx

  5. Ben, that still gives me an error that the file is in use by another process, even though I can open it in Windows.

  6. I beleive that you want to do something like this

    using( FileStream s = File.OpenRead(filename))
    {
    using(TextReader reader = new StreamReader(s))
    {
    // Do smoething
    }
    }

    This ensures the both streams get closed properly.

    Cheers
    Ben

  7. MarkJoel60 says:

    @Ash: I believe you are incorrect. I am using VS2010, and this works:

    System.IO.StreamReader streamReader = new System.IO.StreamReader(System.IO.File.OpenRead(“myfile.txt”));

  8. File.OpenRead(file) .. doesn’t seem to work in VS 2010 C#. The only option (in Intellisense) I get is file.OpenFile no file.openRead. Any thoughts?

  9. Hi Ryan,
    Sorry, don’t know about performance, but I’d guess opening in ReadOnly has not perf impact.
    -Peter

  10. Nice, I did not know of that static method.

    Do you know if it improves performance/memory if opening a file for reading purposes only vs. opening it in RW mode?

  11. Thanks Peter, I was just looking for something like this!

Follow

Get every new post delivered to your Inbox

Join other followers: