Csharp/CSharp Tutorial/Development/Console Redirect

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Console.Out is a TextWriter

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.rupression;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
public class MainClass
{
    public static void Main()
    {
        using (TextWriter tw = Console.Out)
        {
            tw.Write(302.30m); 
            tw.Write("str"); 
            tw.Write(true); 
            tw.WriteLine(".");
        }
    }
}
302.30strTrue.

Redirect Console.Out

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