Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,636 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?