System.Console.SetOut 方法

方法描述

将 Out 属性设置为指定的 TextWriter 对象。

语法定义(C# System.Console.SetOut 方法 的用法)

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static void SetOut(
	TextWriter newOut
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
newOut System-IO-TextWriter 一个流,它是新的标准输出。
返回值 void

提示和注释

默认情况下,Out 属性会设置为标准输出流。

可以使用封装 FileStream 的 StreamWriter 向文件发送输出。 例如:

C#

C++

VB

复制

Console.WriteLine("Hello World");

FileStream fs = new FileStream("Test.txt", FileMode.Create);

// First, save the standard output.

TextWriter tmp = Console.Out;

StreamWriter sw = new StreamWriter(fs);

Console.SetOut(sw);

Console.WriteLine("Hello file");

Console.SetOut(tmp);

Console.WriteLine("Hello World");

sw.Close();

注意

应用到此类型或成员的 HostProtectionAttribute 特性具有以下 Resources 属性值:UI。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标、键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护特性。

System.Console.SetOut 方法例子

下面的代码示例演示 SetOut 方法的用法。

using System;
using System.IO;

public class InsertTabs {
    private const int tabSize = 4;
    private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
    public static int Main(string[] args) {
        StreamWriter writer = null;

        if (args.Length < 2) {
            Console.WriteLine(usageText);
            return 1;
        }

        try {
            // Attempt to open output file.
            writer = new StreamWriter(args[1]);
            // Redirect standard output from the console to the output file.
            Console.SetOut(writer);
            // Redirect standard input from the console to the input file.
            Console.SetIn(new StreamReader(args[0]));
        }
        catch(IOException e) {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;            
        }
        string line;
        while ((line = Console.ReadLine()) != null) {
            string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
            Console.WriteLine(newLine);
        }
        writer.Close();
        // Recover the standard output stream so that a 
        // completion message can be displayed.
        StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
        Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);
        return 0;
    }
}

异常

异常 异常描述
ArgumentNullException newOut 为 null。
SecurityException 调用方没有所要求的权限。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。