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

Valentina Peric
Valentina Peric
10,651 Points

write the contents of the first line of a file to the console

help

CodeChallenge.cs
DirectoryInfo directory = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory());
string fileName = Path.Combine(directory.FullName, "secretmessage.txt");

var file = new FileInfo(fileName);

if (file.Exists)
{
    using ( var reader = new StreamReader(fileName))
    {
            //Console.SetIn(reader); 
            //Console.WriteLine(Console.Contents(fileName));
        //Console.Contents(fileName);
        Console.WriteLine(Console.Contents(reader));

    }
}

1 Answer

Steven Parker
Steven Parker
229,786 Points

System.Console has no Contents method.

But it looks like you had part of the right idea from the commented-out lines. One you select your streamreader using SetIn, you can then use ReadLine to get a line from it:

            Console.SetIn(reader); 
            Console.WriteLine(Console.ReadLine());