I have been trying to get out a demo for MVC 6.0 and I find that I can't read a file anymore using StreamReader
class as it doesn't accept a string anymore.
So code like this
StreamReader reader= new StreamReader("filename.txt")
is not valid?
I am using .NET Core 1.0
StreamReader is in the System.IO namespace. You can add this namespace at the top of your code by doing the following- Alternatively, you could fully qualify all instances of StreamReader like so- System.IO.StreamReader sr = new System.IO.StreamReader (filename);
Creating a StreamReader using FileInfo.OpenText. 1 FileInfo fi = new FileInfo (fileName); 2 StreamReader sr = fi.OpenText (); 3 string s = ""; 4 while ( (s = sr.ReadLine ()) != null) {. 5 Console.WriteLine (s);
A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket. StreamReader reads characters from a byte stream in a particular encoding.
Windows Forms doesn't normally feature a Console. Go on your design surface and drag a textbox. Then come back to your code and change Console.Writeline (line) to textBox1.Text = line.ToString (); StreamReader is in the System.IO namespace. You can add this namespace at the top of your code by doing the following-
I think they've removed it as a StreamReader shouldn't be responsible for creating streams - it's a violation of the Single Responsibility Principle.
You'll need to create a FileStream or similar in order to get the same functionality
using (var stream = new FileStream(@"C:\temp\test.txt", FileMode.Open))
using (var reader = new StreamReader(stream))
{
// do stuff.
}
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