Redirect Console.Out : Console Redirect « Development « C# / CSharp Tutorial






using System;  
using System.IO; 
  
class MainClass { 
  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 ; 
    } 
    
    Console.SetOut(log_out); 
    Console.WriteLine("This is the start of the log file."); 
 
    log_out.Close(); 
  } 
}








14.7.Console Redirect
14.7.1.Redirect Console.Out
14.7.2.Console.Out is a TextWriter