Console.OpenStandardError Console.OpenStandardError Console.OpenStandardError Console.OpenStandardError Method

Définition

Acquiert le flux d'erreur standard.Acquires the standard error stream.

Surcharges

OpenStandardError() OpenStandardError() OpenStandardError() OpenStandardError()

Acquiert le flux d'erreur standard.Acquires the standard error stream.

OpenStandardError(Int32) OpenStandardError(Int32) OpenStandardError(Int32) OpenStandardError(Int32)

Acquiert le flux d'erreur standard, défini sur une taille de mémoire tampon spécifiée.Acquires the standard error stream, which is set to a specified buffer size.

OpenStandardError() OpenStandardError() OpenStandardError() OpenStandardError()

Acquiert le flux d'erreur standard.Acquires the standard error stream.

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

Retours

Flux d'erreur standard.The standard error stream.

Exemples

L’exemple suivant est une visionneuse de fichier texte simple qui affiche le contenu d’un ou plusieurs fichiers texte sur la console.The following example is a simple text file viewer that displays the contents of one or more text files to the console. S’il n’y a pas d’arguments de ligne de commande, ou si des fichiers passés comme arguments de ligne de commande n' SetError existent pas, l’exemple appelle la méthode pour rediriger les OpenStandardError informations d’erreur vers un fichier, appelle la méthode dans le processus de réacquisition de l’erreur standard. Stream, et indique que les informations d’erreur ont été écrites dans un fichier.If there are no command line arguments, or if any files passed as command line arguments do not exist, the example calls the SetError method to redirect error information to a file, calls the OpenStandardError method in the process of reacquiring the standard error stream, and indicates that error information was written to a file.

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.
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.

Notez que la StreamWriter.AutoFlush propriété a la valeur true avant de réacquérir le flux d’erreurs.Note that the StreamWriter.AutoFlush property is set to true before reacquiring the error stream. Cela permet de s’assurer que la sortie est envoyée à la console immédiatement au lieu de la mise en mémoire tampon.This ensures that output will be sent to the console immediately rather than buffered.

Remarques

Cette méthode peut être utilisée pour récupérer le flux d’erreurs standard après qu’il a été modifié par SetError la méthode.This method can be used to reacquire the standard error stream after it has been changed by the SetError method.

Voir aussi

OpenStandardError(Int32) OpenStandardError(Int32) OpenStandardError(Int32) OpenStandardError(Int32)

Acquiert le flux d'erreur standard, défini sur une taille de mémoire tampon spécifiée.Acquires the standard error stream, which is set to a specified buffer size.

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

Paramètres

bufferSize
Int32 Int32 Int32 Int32

Taille de la mémoire tampon du flux interne.The internal stream buffer size.

Retours

Flux d'erreur standard.The standard error stream.

Exceptions

bufferSize est inférieur ou égal à zéro.bufferSize is less than or equal to zero.

Remarques

Cette méthode peut être utilisée pour récupérer le flux d’erreurs standard après qu’il a été modifié par SetError la méthode.This method can be used to reacquire the standard error stream after it has been changed by the SetError method.

Voir aussi

S’applique à