tags:

views:

5688

answers:

9

I'm trying to read the contents of a text file, in this case a list of computer names (Computer1, computer2 etc,) and I thought that StreamReader would be what you would use but when I do the following:

StreamReader arrComputer = new StreamReader(FileDialog.filename)();

I get this error:

The type or namespace name 'StreamReader' could not be found (are you missing a using directive or an assembly reference?)

I'm very new to C# so I'm sure I'm making a newbie mistake.

+2  A: 

try

using System.IO;


StreamReader arrComputer = new StreamReader(FileDialog.filename);
Steven A. Lowe
Thanks! That fixed it. I saw your reply earlier and totally missed the using System.IO; namespace...
Jim
+7  A: 

You need to import the System.IO namespace. Put this at the top of your .cs file:

using System.IO;

Either that, or explicitly qualify the type name:

System.IO.StreamReader arrComputer = new System.IO.StreamReader(FileDialog.filename);

HTH, Kent

Kent Boogaart
Wow! That helps! Can't believe I missed that. Man, this place rocks! thanks to everyone who pointed this out to me. Perfect!
Jim
+1  A: 

Make sure you include using System.IO in the usings declaration

Werg38
+1  A: 

Make sure you are have "using System.IO;" at the top of your module. Also, you don't need the extra parenthesis at the end of "new StreamReader(FileDialog.filename)".

Gene
+1  A: 

Make sure you have the System assembly in your reference of the project and add this to the using part:

using System.IO;
CheGueVerra
Funny I didn't received the Load new answers ...
CheGueVerra
+1  A: 

StreamReader is defined in System.IO. You either need to add

using System.IO;

to the file or change your code to:

System.IO.StreamReader arrComputer = new System.IO.StreamReader(FileDialog.filename);
alabamasucks
+4  A: 

You'll need:

using System.IO;

At the top of the .cs file. If you're reading text content I recommend you use a TextReader which is bizarrely a base class of StreamReader.

try:

using(TextReader reader = new StreamReader(/* your args */))
{
}

The using block just makes sure it's disposed of properly.

Quibblesome
+1  A: 

You need to add a reference to the System.IO assembly. You can do this via the "My Project" properties page under the References tab.

NYSystemsAnalyst
My Project only exists in Visual Basic.NET projects.
Dana Robinson
A: 

So, what's the answer? I couldn't understand from the 50000 replies from all the n00bs who love to answer the easy questions, but not the hard.