Mega Code Archive

 
Categories / C# by API / System
 

Console SetOut

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