I try to "wrap" a StreamReader
in a class Fichier
, with some extra methods and attributes.
I want two things :
StreamReader
opens automatically in the Fichier
class;StreamReader
opens when we use the overrided method ReadLine
, and not before (we need to modify the file before reading it with a StreamReader
).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.
The easiest thing is to Dispose
(you really should do that, it closes the stream too) it and set it to null
when you close it.
Optionally you could check for EndOfStream
, but that requires you to read to the end of the stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With