Bug in StreamReader?

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

When using a StreamReader I cannot open files that are currently opened from
another application and are therefore readonly. But I can open these files
in notepad.
It seems that StreamReader tries to open the file in writemode which is then
denied an an exception occures.
 
Hi Cody,

Change your StreamReader constructor to

StreamReader sr = new StreamReader(File.OpenRead(path));

If you also need to save data to the file, you could store all data in
memory while processing and try to obtain write access later.
 
Change your StreamReader constructor to
StreamReader sr = new StreamReader(File.OpenRead(path));

If you also need to save data to the file, you could store all data in
memory while processing and try to obtain write access later.

Yes this would certainly solve the problem but it is really a bug in the
StreamReaderclass, is it? Iam using the following ctor:
StreamReader(string,Encoding).
 
Well, there is nothing in the documentations saying that it opens a file
as read only, so the name StreamReader may be misleading, and in fact, the
'path' may not even lead to a conventional file.
I would not say it was a bug, though it may have been unintentional, or
intentional as maybe having it read only as default may cause other
problems.
 
Well, there is nothing in the documentations saying that it opens a file
as read only, so the name StreamReader may be misleading, and in fact, the
'path' may not even lead to a conventional file.
I would not say it was a bug, though it may have been unintentional, or
intentional as maybe having it read only as default may cause other
problems.

Which other problems? It is a *reader*, which problems could be caused it I
have readoly access to the file?
 
Back
Top