Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type or namespace name "StreamReader" could not be found

I am working on StreamReader in C#. I am getting the error

"The type or namespace name "StreamReader" could not be found"

I have no idea what I am doing wrong.

using System.IO;
using System;
class Perfect
{
static void Main()
    {
    string filename = @"marks.cvs";
    StreamReader sr = new StreamReader(filename);
    string line = sr.ReadLine();
    Console.WriteLine(line);    
    sr.Close();
    }
}
like image 449
maddddie123 Avatar asked Aug 30 '15 02:08

maddddie123


People also ask

What does StreamReader mean?

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file. Important. This type implements the IDisposable interface.

What does the following constructor do public StreamReader stream stream?

This constructor initializes the encoding as specified by the encoding parameter, the BaseStream property using the stream parameter, and the internal buffer size to 1024 bytes. The detectEncodingFromByteOrderMarks parameter detects the encoding by looking at the first four bytes of the stream.


2 Answers

StreamReader is in the System.IO namespace. You can add this namespace at the top of your code by doing the following-

using System.IO;

Alternatively, you could fully qualify all instances of StreamReader like so-

System.IO.StreamReader sr = new System.IO.StreamReader(filename);

But this may get a little tedious, especially if you end up using other objects in System.IO. Therefore I would recommend going with the former.

More on namespaces and the using directive-

https://msdn.microsoft.com/en-us/library/0d941h9d.aspx

https://msdn.microsoft.com/en-us/library/sf0df423.aspx

like image 50
iliketocode Avatar answered Sep 22 '22 12:09

iliketocode


StreamReader requires a namespace which you are missing. Add these two at top of the .cs file.

using System;
using System.IO;
StreamReader sr = new StreamReader(filename);

Its always a best-practice to add namespace at top of the file. However, you can add like this System.IO.StreamReader mentioned by @iliketocode.

System.IO.StreamReader sr = new System.IO.StreamReader(filename);
like image 23
HaveNoDisplayName Avatar answered Sep 22 '22 12:09

HaveNoDisplayName