I try to "wrap" a StreamReader in a class Fichier, with some extra methods and attributes.
I want two things :
- The
StreamReaderopens automatically in theFichierclass; - The
StreamReaderopens when we use the overrided methodReadLine, and not before (we need to modify the file before reading it with aStreamReader).
A piece of my code looks like this :
public string ReadLine() { if (Reader == null || ???) { Reader = new StreamReader(Path); } return Reader.ReadLine(); } At ???, I want to check if the StreamReader has been closed. Indeed, if we do :
StreamReader sr = new StreamReader(path); sr.Close(); sr is not null, but how to check that it is closed, and how to re-open it ?
If you wonder why I need to open and close StreamReader, it is because the Fichier object needs to exist anytime, but the file that it represents needs to be modified several times in an external program.
System.IOnamespace has a class namedFileso be careful that you don't mix up your own class and theSystem.IO.Fileclass..BaseStream.CanReadperhaps, although explicitly disposing/null would be better.