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

Dominik Huber
Dominik Huber
4,631 Points

Can someone please explain this to me in more details? Console.WriteLine(Console.ReadLine()) makes no sense to me.

HI guys,

I have a few questions.

So the first code I don't get is this:

What exactly does the file.FullName return? Documentation says it returns the full path of the file. But why? I think we already have the full path with this code:

fileName = Path.Combine(directory.FullName, "data.txt");

Next question:

Console.SetIn(reader); --> What is this doing exactly? It passes in the "Stream" and does what exactly?

So whenever I call Console.ReadLine() now I can't write anything to it, because the "StreamReader" passes in the string from the data.txt file? And it gets passed in every time I call Console.ReadLine(), until I close the stream? Please if someone could explain me this like I'm five would be very nice.

Thx guys

3 Answers

Steven Parker
Steven Parker
231,275 Points

The "SetIn" method determines where the input will come from. It's like setting a switch. If you choose a stream, it will take the input from the stream instead of from typing to the actual console.

If you want stream input and console input you would not redirect the console. Instead you can use the read methods of the stream itself.

Andrei Li
Andrei Li
34,486 Points

So, if I want back to read a line from the keyboard I have to switch it back to keyboard input. Is it correct? What command switches it back to keyboard input?

Steven Parker
Steven Parker
231,275 Points

As I said, it would probably make more sense to use the stream methods for input instead of redirecting if you need it back later. But you could save the original and restore it later if necessary:

TextReader original = Console.In;
// code where you use SetIn to read from a file...
Console.SetIn(original);  // reset the input
James Churchill
STAFF
James Churchill
Treehouse Teacher

Dominik,

In order to retrieve the information for the "data.txt" file, we need the full path to that file, which is what this code is doing:

var fileName = Path.Combine(directory.FullName, "data.txt");

Then, after we've retrieved the file information and verified that the file exists, we can use the FileInfo object's FullName property to get the full path to the file. As you've noted, we already had the value on hand in the fileName variable, so using the FileInfo FullName property later on is a bit redundant.

I hope that helps.

Thanks ~James

Andrei Li
Andrei Li
34,486 Points

Thank you, Steven! You are the person who adds value to Treehouse!