Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New StreamReader class doesn't accept filename

Tags:

c#

asp.net

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

like image 779
Ashutosh Avatar asked Jun 13 '16 08:06

Ashutosh


People also ask

How to get the namespace of streamreader in Java?

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);

How to create a streamreader using FILEINFO?

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);

What is the difference between a stream and streamreader?

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.

How do I add a streamreader to a textbox?

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-


1 Answers

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.
}
like image 144
RB. Avatar answered Sep 22 '22 08:09

RB.