Advertisement
BaSs_HaXoR

Redirecting the Console output to a file

Sep 11th, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. //Write Console (all lines) to .txt document:
  2. static public void Main ()
  3. {
  4.     FileStream ostrm;
  5.     StreamWriter writer;
  6.     TextWriter oldOut = Console.Out;
  7.     try
  8.     {
  9.         ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
  10.         writer = new StreamWriter (ostrm);
  11.     }
  12.     catch (Exception e)
  13.     {
  14.         Console.WriteLine ("Cannot open Redirect.txt for writing");
  15.         Console.WriteLine (e.Message);
  16.         return;
  17.     }
  18.     Console.SetOut (writer);
  19.     Console.WriteLine ("This is a line of text");
  20.     Console.WriteLine ("Everything written to Console.Write() or");
  21.     Console.WriteLine ("Console.WriteLine() will be written to a file");
  22.     Console.SetOut (oldOut);
  23.     writer.Close();
  24.     ostrm.Close();
  25.     Console.WriteLine ("Done");
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement