does StreamReader need exclusive access to the file?

  • Thread starter Thread starter Daniel P.
  • Start date Start date
D

Daniel P.

I'm using StreamReader to read a text file and sometimes I get an error
saying that the file is opened by someone else.

I haven't found any info about how to set a flag to tell StreamReader to
open the file in shared mode and read mode.

Thanks!
Daniel
 
Hi Daniel,

Strema reader uses new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.Read) if you don't pass the already stream in the constructor.
You might pass a valid stream object in the constructor of StreamReader if
you feel like opening the file differently.
And, don't forget to close it.
 
Daniel,

In this case, you will want to use the FileStream object and open the
file yourself with the appropriate access. Once you have that, you can pass
the FileStream instance to the StreamReader constructor.

Hope this helps.
 
Thanks Nicholas!
Thanks Miha!

Do I close both the StreamReader and FileStream?

If I use
using( ... )
{


}

do I still have to close it or Dispose will close the file automatically?

Thanks!
 
Hi Daniel,

I think that closing StreamReader is enough.
Also, "using" on streamreader is enough.
 
Back
Top