Console.OpenStandardError 메서드

정의

표준 오류 스트림을 가져옵니다.

오버로드

OpenStandardError()

표준 오류 스트림을 가져옵니다.

OpenStandardError(Int32)

표준 오류 스트림을 가져와 지정한 버퍼 크기로 설정합니다.

OpenStandardError()

표준 오류 스트림을 가져옵니다.

public:
 static System::IO::Stream ^ OpenStandardError();
public static System.IO.Stream OpenStandardError ();
static member OpenStandardError : unit -> System.IO.Stream
Public Shared Function OpenStandardError () As Stream

반환

Stream

표준 오류 스트림입니다.

예제

다음 예제는 콘솔에 하나 이상의 텍스트 파일의 콘텐츠를 표시 하는 간단한 텍스트 파일 뷰어입니다. 명령줄 인수가 없거나 명령줄 인수로 전달된 파일이 없는 경우 예제에서는 메서드를 호출 SetError 하여 오류 정보를 파일로 리디렉션하고, 표준 오류 스트림을 다시 가져오는 과정에서 메서드를 호출 OpenStandardError 하고, 오류 정보가 파일에 기록되었음을 나타냅니다.

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.
open System
open System.IO

let args = Environment.GetCommandLineArgs()[1..]
let mutable errorOutput = ""
// Make sure that there is at least one command line argument.
if args.Length < 1 then
    errorOutput <- errorOutput + "You must include a filename on the command line.\n"

for file in args do
    // Check whether the file exists.
    if File.Exists file then
        errorOutput <- errorOutput + $"'{file}' does not exist.\n"
    else
        // Display the contents of the file.
        use sr = new StreamReader(file)
        let contents = sr.ReadToEnd()
        Console.WriteLine $"*****Contents of file '{file}':\n\n"
        Console.WriteLine contents
        Console.WriteLine "*****\n"

// Check for error conditions.
if not (String.IsNullOrEmpty errorOutput) then
    // 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.
    use 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.
Imports System.IO

Module ViewTextFile
   Public Sub Main()
      Dim args() As String = Environment.GetCommandLineArgs()
      Dim errorOutput As String = ""
      ' Make sure that there is at least one command line argument.
      If args.Length <= 1 Then
         errorOutput += "You must include a filename on the command line." +
                        vbCrLf
      End If
      
      For ctr As Integer = 1 To args.GetUpperBound(0)
         ' Check whether the file exists.
         If Not File.Exists(args(ctr)) Then
            errorOutput += String.Format("'{0}' does not exist.{1}",
                                         args(ctr), vbCrLf)
         Else
            ' Display the contents of the file.
            Dim sr As New StreamReader(args(ctr))
            Dim contents As String = sr.ReadToEnd()
            sr.Close()
            Console.WriteLine("***** Contents of file '{0}':{1}{1}",
                              args(ctr), vbCrLf)
            Console.WriteLine(contents)
            Console.WriteLine("*****{0}", vbCrLf)
         End If
      Next

      ' Check for error conditions.
      If Not String.IsNullOrEmpty(errorOutput) Then
         ' 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.
         Dim standardError As New StreamWriter(Console.OpenStandardError())
         standardError.AutoFlush = True
         Console.SetError(standardError)
         Console.Error.WriteLine("{0}Error information written to ViewTextFile.Err.txt",
                                 vbCrLf)
      End If
   End Sub
End Module
' 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.

StreamWriter.AutoFlush 속성은 오류 스트림을 true 다시 가져오기 전에 설정됩니다. 이렇게 하면 출력이 버퍼링되지 않고 즉시 콘솔로 전송됩니다.

설명

이 메서드는 메서드에 의해 SetError 변경된 후 표준 오류 스트림을 다시 가져오는 데 사용할 수 있습니다.

추가 정보

적용 대상

OpenStandardError(Int32)

표준 오류 스트림을 가져와 지정한 버퍼 크기로 설정합니다.

public:
 static System::IO::Stream ^ OpenStandardError(int bufferSize);
public static System.IO.Stream OpenStandardError (int bufferSize);
static member OpenStandardError : int -> System.IO.Stream
Public Shared Function OpenStandardError (bufferSize As Integer) As Stream

매개 변수

bufferSize
Int32

이 매개 변수는 영향을 주지 않지만 해당 값은 0보다 크거나 같아야 합니다.

반환

Stream

표준 오류 스트림입니다.

예외

bufferSize가 0보다 작거나 같습니다.

설명

이 메서드는 메서드에 의해 SetError 변경된 후 표준 오류 스트림을 다시 가져오는 데 사용할 수 있습니다.

추가 정보

적용 대상