Mega Code Archive

 
Categories / C# / Development Class
 

Redirect Console Out

/* C#: The Complete Reference  by Herbert Schildt  Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Redirect Console.Out.    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);        Console.WriteLine("This is the end of the log file.");      log_out.Close();    }  }