英語で読む

次の方法で共有


Console.OpenStandardError メソッド

定義

標準エラー ストリームを取得します。

オーバーロード

OpenStandardError()

標準エラー ストリームを取得します。

OpenStandardError(Int32)

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

OpenStandardError()

標準エラー ストリームを取得します。

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

戻り値

Stream

標準エラー ストリーム。

次の例は、1 つ以上のテキスト ファイルの内容をコンソールに表示する単純なテキスト ファイル ビューアーです。 コマンド ライン引数がない場合、またはコマンド ライン引数として渡されたファイルが存在しない場合は、メソッドを SetError 呼び出してエラー情報をファイルにリダイレクトし、標準エラー ストリームを再取得する処理中にメソッドを呼び出 OpenStandardError し、エラー情報がファイルに書き込まれたことを示します。

C#
using System;
using System.IO;

public class ViewTextFile
{
   public static void Main()
   {
      String[] args = Environment.GetCommandLineArgs();
      String errorOutput = "";
      // Make sure that there is at least one command line argument.
      if (args.Length <= 1)
         errorOutput += "You must include a filename on the command line.\n";

      for (int ctr = 1; ctr <= args.GetUpperBound(0); ctr++)  {
         // Check whether the file exists.
         if (! File.Exists(args[ctr])) {
            errorOutput += String.Format("'{0}' does not exist.\n", args[ctr]);
         }
         else {
            // Display the contents of the file.
            StreamReader sr = new StreamReader(args[ctr]);
            String contents = sr.ReadToEnd();
            sr.Close();
            Console.WriteLine("*****Contents of file '{0}':\n\n",
                              args[ctr]);
            Console.WriteLine(contents);
            Console.WriteLine("*****\n");
         }
      }

      // Check for error conditions.
      if (! String.IsNullOrEmpty(errorOutput)) {
         // Write error information to a file.
         Console.SetError(new StreamWriter(@".\ViewTextFile.Err.txt"));
         Console.Error.WriteLine(errorOutput);
         Console.Error.Close();
         // Reacquire the standard error stream.
         var standardError = new StreamWriter(Console.OpenStandardError());
         standardError.AutoFlush = true;
         Console.SetError(standardError);
         Console.Error.WriteLine("\nError information written to ViewTextFile.Err.txt");
      }
   }
}
// If the example is compiled and run with the following command line:
//     ViewTextFile file1.txt file2.txt
// and neither file1.txt nor file2.txt exist, it displays the
// following output:
//     Error information written to ViewTextFile.Err.txt
// and writes the following text to ViewTextFile.Err.txt:
//     'file1.txt' does not exist.
//     'file2.txt' does not exist.

このプロパティは、エラー ストリームをtrue再び受け取る前に設定されていることStreamWriter.AutoFlushに注意してください。 これにより、出力はバッファーではなく、すぐにコンソールに送信されます。

注釈

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

こちらもご覧ください

適用対象

.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

OpenStandardError(Int32)

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

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

パラメーター

bufferSize
Int32

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

戻り値

Stream

標準エラー ストリーム。

例外

bufferSize が 0 以下です。

注釈

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

こちらもご覧ください

適用対象

.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