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

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
212,257 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.

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
212,257 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

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