Q:

C# method to read text error log file

I am trying to read a file I create that contains all the logs lines throughout my program. I have the following cod:
    private string ReadEmailLog(string EmailLog)
    {
        TextReader tr = new StreamReader(EmailLog);
        tr.ReadLine();
        tr.Close();
    }

I need to read the EmailLog file, every line of it, and then put return it into a string called message. How would I get this method to return the whole log file, every line?

A:

You can use File.ReadAllText or File.ReadAllLines.
If you're using .NET 4.0, you can also use File.ReadLines:
var files = from file in Directory.EnumerateFiles(@"c:\",
                "*.txt", SearchOption.AllDirectories)
            from line in File.ReadLines(file)
            where line.Contains("Microsoft")
            select new
            {
                File = file,
                Line = line
            };

foreach (var f in files)
{
    Console.WriteLine("{0}\t{1}", f.File, f.Line);
}

This allows you to make file I/O part of a LINQ operation.

