Sdílet prostřednictvím


Console.OpenStandardError Metoda

Definice

Získá standardní datový proud chyb.

Přetížení

Name Description
OpenStandardError()

Získá standardní datový proud chyb.

OpenStandardError(Int32)

Získá standardní chybový proud, který je nastavený na zadanou velikost vyrovnávací paměti.

OpenStandardError()

Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs

Získá standardní datový proud chyb.

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

Návraty

Standardní datový proud chyb.

Příklady

Následující příklad je jednoduchý prohlížeč textových souborů, který zobrazuje obsah jednoho nebo více textových souborů do konzoly. Pokud neexistují žádné argumenty příkazového řádku nebo pokud žádné soubory předané jako argumenty příkazového řádku neexistují, příklad volá SetError metodu pro přesměrování informací o chybě do souboru, volá OpenStandardError metodu v procesu opětovného vyvolání standardního chybového streamu a označuje, že informace o chybě byly zapsány do souboru.

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.

Všimněte si, že StreamWriter.AutoFlush vlastnost je nastavena na true před opětovnou aktivací datového proudu chyby. Tím se zajistí, že se výstup odešle do konzoly okamžitě místo uložení do vyrovnávací paměti.

Poznámky

Tuto metodu lze použít ke změně standardního datového proudu chyb po změně metodou SetError .

Viz také

Platí pro

OpenStandardError(Int32)

Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs
Zdroj:
Console.cs

Získá standardní chybový proud, který je nastavený na zadanou velikost vyrovnávací paměti.

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

Parametry

bufferSize
Int32

Tento parametr nemá žádný vliv, ale jeho hodnota musí být větší nebo rovna nule.

Návraty

Standardní datový proud chyb.

Výjimky

bufferSize je menší než nebo rovno nule.

Poznámky

Tuto metodu lze použít ke změně standardního datového proudu chyb po změně metodou SetError .

Viz také

Platí pro