Console::Error Property

 

Gets the standard error output stream.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public:
property TextWriter^ Error {
	[HostProtectionAttribute(SecurityAction::LinkDemand, UI = true)]
	static TextWriter^ get();
}

Property Value

Type: System.IO::TextWriter^

A TextWriter that represents the standard error output stream.

This standard error stream is set to the console by default. It can be set to another stream with the SetError method. After the standard error stream is redirected, it can be reacquired by calling the OpenStandardError method.

In console applications whose informational output is often redirected to a file, the standard error stream available through the Error property can be used to display information to the console even if output is redirected. The following example displays product tables for 10 numbers at a time starting with 1. After every set of 10 numbers, the Error property is used to ask the user whether to display the next set. If the standard output is redirected to a file, the user is still asked whether the routine should generate the next set of products.

No code example is currently available or this language may not be supported.

The following example is a command line utility named ExpandTabs that replaces tab characters in a text file with four spaces, the value defined by the tabSize variable. It redirects the standard input and output streams to files, but uses the Error property to write the standard error stream to the console. It can be launched from the command line by supplying the name of the file that contains tab characters and the name of the output file.

using namespace System;
using namespace System::IO;

void main()
{
   const int tabSize = 4;
   array<String^>^args = Environment::GetCommandLineArgs();
   String^ usageText = "Usage: EXPANDTABSEX inputfile.txt outputfile.txt";
   StreamWriter^ writer = nullptr;

   if ( args->Length < 3 )
   {
      Console::WriteLine( usageText );
      return;
   }

   try
   {
      writer = gcnew StreamWriter( args[ 2 ] );
      Console::SetOut( writer );
      Console::SetIn( gcnew StreamReader( args[ 1 ] ) );
   }
   catch ( IOException^ e ) 
   {
      TextWriter^ errorWriter = Console::Error;
      errorWriter->WriteLine( e->Message );
      errorWriter->WriteLine( usageText );
      return;
   }

   int i;
   while ( (i = Console::Read()) != -1 )
   {
      Char c = (Char)i;
      if ( c == '\t' )
            Console::Write( ((String^)"")->PadRight( tabSize, ' ' ) );
      else
            Console::Write( c );
   }

   writer->Close();

   // Recover the standard output stream so that a 
   // completion message can be displayed.
   StreamWriter^ standardOutput = gcnew StreamWriter(Console::OpenStandardOutput());
   standardOutput->AutoFlush = true;
   Console::SetOut(standardOutput);
   Console::WriteLine( "EXPANDTABSEX has completed the processing of {0}.", args[ 0 ] );
   return;
}

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.

No code example is currently available or this language may not be supported.

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.

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: