Example:
variable = new StreamReader( file ).ReadToEnd();
Is that acceptable?
Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.
StreamReader. ReadLine() method reads a line of characters from the current stream and returns the data as a string.
A StreamReader is a TextReader which means it is a Stream wrapper. A TextReader will convert (or encode) Text data (string or char) to byte[] and write them down to the underlying Stream .
No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it's GC'd sooner):
using (StreamReader r = new StreamReader("file.txt")) { allFileText = r.ReadToEnd(); }
Or alternatively in .Net 2 you can use the new File. static members, then you don't need to close anything:
variable = File.ReadAllText("file.txt");
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