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

Calvin Secrest
Calvin Secrest
24,815 Points

Use the provided fileName variable to create a new StreamReader object named reader. Wrap the reader object with a using

Use the provided fileName variable to create a new StreamReader object named reader. Wrap the reader object with a using statement to ensure it is properly closed.

I think I'm pretty close but can't recognize the syntax, please would like someone's input on this matter?

CodeChallenge.cs
DirectoryInfo directory = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory());
string fileName = Path.Combine(directory.FullName, "secretmessage.txt"); 
{
    var reader = new StreamReader(file.FullName);  
    Console.SetIn(reader);
}

3 Answers

Steven Parker
Steven Parker
231,275 Points

:point_right: You forgot to "Wrap the reader object with a using"

Plus, you didn't "Use the provided fileName variable" but somehow came up with something not defined.
Anyway, doing both will give you something like this:

using (var reader = new StreamReader(fileName))  
{
}

Also note that at this step, there's no contents yet inside the using.

DirectoryInfo directory = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory()); string fileName = Path.Combine(directory.FullName, "secretmessage.txt"); using (var reader = new StreamReader(fileName))
{ }

Calvin Secrest
Calvin Secrest
24,815 Points

Thanks for your response