Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Streams and Data Processing Reading Data Console I/O

how to use ReadLine() (from the prompt) after we closed a file. After using the SetIn function it doesn't work.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;

namespace SoccerStats { class Program { static void Main(string[] args) { string currDirectory = Directory.GetCurrentDirectory(); DirectoryInfo info = new DirectoryInfo(currDirectory);

        var filename = Path.Combine(info.FullName, "data.txt");
        var file = new FileInfo(filename);

        if(file.Exists)
        {
            using (var reader = new StreamReader(file.FullName))
            {
                Console.SetIn(reader);
                Console.WriteLine(Console.ReadLine());
            }
        }

        Console.ReadLine();

    }
}

}

1 Answer

Emmanuel C
Emmanuel C
10,593 Points

The console was set to the ReadStreamer reader, with SetIn method. but reader gets closed when it goes out of scope of the using statement. Can probably set console back to standard input(prompt) with the OpenStandardInput() method of console class.

Richard Külling
Richard Külling
9,465 Points

I tried that, but failed. Could someone post a working example of this?