try following code this is tested application, it works.
the problem was that u have redirected the console to
write to file, after that u wanted to display string on
console screen, i think u might have error that sreamWriter
is closed and cannot write to closed streamWriter.
to solve problem, u must return console to write to standard
output sream. so this is done in following code.

using System;
using System.IO;
public class Redirect
{
    public static void Main()
    {
        StreamWriter log_out;
        try
        {
            log_out = new StreamWriter("logfile.txt");
        }
        catch (IOException exc)
        {
            Console.WriteLine(exc.Message + "Cannot open file.");
            return;
        } // Direct standard output to the log file.
        Console.SetOut(log_out);
        Console.WriteLine("This is the start of the log file.");
        for (int i = 0; i < 10; i++)
            Console.WriteLine(i);
        log_out.Close();
        log_out = new StreamWriter(Console.OpenStandardOutput());
        log_out.AutoFlush = true;
        Console.SetOut(log_out);
        Console.WriteLine("This is the end of the log file.");
        Console.ReadLine();
    }
}

Reply via email to