Console.OpenStandardError Method

Definition

Acquires the standard error stream.

Overloads

OpenStandardError()

Acquires the standard error stream.

OpenStandardError(Int32)

Acquires the standard error stream, which is set to a specified buffer size.

OpenStandardError()

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

Acquires the standard error stream.

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

Returns

The standard error stream.

Examples

The following example is a simple text file viewer that displays the contents of one or more text files to the console. 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.

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.

Note that the StreamWriter.AutoFlush property is set to true before reacquiring the error stream. This ensures that output will be sent to the console immediately rather than buffered.

Remarks

This method can be used to reacquire the standard error stream after it has been changed by the SetError method.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

OpenStandardError(Int32)

Source:
Console.cs
Source:
Console.cs
Source:
Console.cs

Acquires the standard error stream, which is set to a specified buffer size.

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

Parameters

bufferSize
Int32

This parameter has no effect, but its value must be greater than or equal to zero.

Returns

The standard error stream.

Exceptions

bufferSize is less than or equal to zero.

Remarks

This method can be used to reacquire the standard error stream after it has been changed by the SetError method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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, 4.8.1
.NET Standard 2.0, 2.1