December 7th, 2009How 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!









December 8th, 2009 at 5:03 am
Thanks Peter, I was just looking for something like this!
December 8th, 2009 at 1:06 pm
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?
December 8th, 2009 at 1:06 pm
Hi Ryan,
Sorry, don’t know about performance, but I’d guess opening in ReadOnly has not perf impact.
-Peter
July 23rd, 2010 at 11:27 am
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?
August 17th, 2011 at 12:47 pm
@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”));
October 6th, 2011 at 2:46 pm
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
March 5th, 2012 at 9:18 am
Ben, that still gives me an error that the file is in use by another process, even though I can open it in Windows.
March 5th, 2012 at 9:34 am
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
April 2nd, 2012 at 2:36 am
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.
April 2nd, 2012 at 4:07 am
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.