Redirect Console.Out : Console Input Output : Development Class C# Source Code


Custom Search

C# Source Code » Development Class » Console Input Output »

 

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(); 
  } 
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Development Class
» Console Input Output