英語で読む

次の方法で共有


Console.OpenStandardOutput メソッド

定義

標準出力ストリームを取得します。

オーバーロード

OpenStandardOutput()

標準出力ストリームを取得します。

OpenStandardOutput(Int32)

指定したバッファー サイズに設定された標準出力ストリームを取得します。

OpenStandardOutput()

標準出力ストリームを取得します。

C#
public static System.IO.Stream OpenStandardOutput ();

戻り値

Stream

標準出力ストリーム。

OpenStandardOutput メソッドの使用例を次に示します。 これにより、文字列内の 4 個の連続する空白文字がタブ文字に置き換えられます。 これを実行するには、2 つのコマンド ライン引数を指定する必要があります。 1 つ目は、標準入力ストリームのリダイレクト先となる既存のテキスト ファイルの名前です。 2 つ目は、標準出力ストリームのリダイレクト先となるファイルの名前です。 このファイルは存在する必要はありません。 ファイルが存在する場合は、その内容が上書きされます。

C#
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)
    {
        if (args.Length < 2)
        {
            Console.WriteLine(usageText);
            return 1;
        }

        try
        {
            // Attempt to open output file.
            using (var writer = new StreamWriter(args[1]))
            {
                using (var reader = new StreamReader(args[0]))
                {
                    // 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(reader);
                    string line;
                    while ((line = Console.ReadLine()) != null)
                    {
                        string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
                        Console.WriteLine(newLine);
                    }
                }
            }
        }
        catch(IOException e)
        {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;
        }

        // Recover the standard output stream so that a
        // completion message can be displayed.
        var standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
        Console.WriteLine($"INSERTTABS has completed the processing of {args[0]}.");
        return 0;
    }
}

注釈

このメソッドは、メソッドによって SetOut 変更された後に標準出力ストリームを再取得するために使用できます。

こちらもご覧ください

適用対象

.NET 7 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

OpenStandardOutput(Int32)

指定したバッファー サイズに設定された標準出力ストリームを取得します。

C#
public static System.IO.Stream OpenStandardOutput (int bufferSize);

パラメーター

bufferSize
Int32

このパラメーターには影響はありませんが、その値は 0 以上である必要があります。

戻り値

Stream

標準出力ストリーム。

例外

bufferSize が 0 以下です。

OpenStandardOutput メソッドの使用例を次に示します。 これにより、文字列内の 4 個の連続する空白文字がタブ文字に置き換えられます。 これを実行するには、2 つのコマンド ライン引数を指定する必要があります。 1 つ目は、標準入力ストリームのリダイレクト先となる既存のテキスト ファイルの名前です。 2 つ目は、標準出力ストリームのリダイレクト先となるファイルの名前です。 このファイルは存在する必要はありません。 ファイルが存在する場合は、その内容が上書きされます。

C#
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)
    {
        if (args.Length < 2)
        {
            Console.WriteLine(usageText);
            return 1;
        }

        try
        {
            // Attempt to open output file.
            using (var writer = new StreamWriter(args[1]))
            {
                using (var reader = new StreamReader(args[0]))
                {
                    // 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(reader);
                    string line;
                    while ((line = Console.ReadLine()) != null)
                    {
                        string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
                        Console.WriteLine(newLine);
                    }
                }
            }
        }
        catch(IOException e)
        {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;
        }

        // Recover the standard output stream so that a
        // completion message can be displayed.
        var standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
        Console.WriteLine($"INSERTTABS has completed the processing of {args[0]}.");
        return 0;
    }
}

注釈

このメソッドは、メソッドによって SetOut 変更された後に標準出力ストリームを再取得するために使用できます。

こちらもご覧ください

適用対象

.NET 7 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1