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
7,638 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,463 Points

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