type or namespace name "StreamReader" could not be found

maddddie123 picture maddddie123 · Aug 30, 2015 · Viewed 9.8k times · Source

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

Answer

iliketocode picture iliketocode · Aug 30, 2015

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