Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Streamreader "The type or namespace name could not be found" error, but I've got the System.IO namespace

I'm brand new to C#, but I think I have the correct "using" statements here, so I presume the problem is somewhere in my class structure or syntax? I'm getting the "The type or namespace name 'Textreader' could not be found" error. Thank you.

using System;
using System.IO;

namespace Layouts.Test_control {

    public partial class Test_controlSublayout : System.Web.UI.UserControl 
    {
        private void Page_Load(object sender, EventArgs e) {

            Textreader tr = new StreamReader("date.txt");

            Console.WriteLine(tr.ReadLine());

            tr.Close();
        }
    }
}
like image 624
Kristi Simonson Avatar asked Dec 27 '22 02:12

Kristi Simonson


1 Answers

C# is case sensitive so you probably want this instead:

TextReader tr = new StreamReader("date.txt");

Apart from that you've mentioned in your question that you would use the correct "using" statements, but obviously you're not disposing/closing the StreamReader at all. You're also reading only one line of the file.

// The using statement also closes the StreamReader.
using(var sr = new StreamReader("date.txt"))
{
    String line;
    while ((line = sr.ReadLine()) != null)
    {
         Console.WriteLine(line);
    }
}
like image 131
Tim Schmelter Avatar answered Apr 06 '23 07:04

Tim Schmelter