Export (0) Print
Expand All

Console.Error Property

Gets the standard error output stream.

[Visual Basic]
Public Shared ReadOnly Property Error As TextWriter
[C#]
public static TextWriter Error {get;}
[C++]
public: __property static TextWriter* get_Error();
[JScript]
public static function get Error() : TextWriter;

Property Value

A TextWriter that represents the standard error output stream.

Remarks

This property is set to the standard error stream by default. This property can be set to another stream with the SetError method.

Example

The following code sample illustrates the use of Error:

[Visual Basic] 
Public Class ExpandTabs
   Private Const tabSize As Integer = 4
   Private Const usageText As String = "Usage: EXPANDTABSEX inputfile.txt outputfile.txt"
   
   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
   End Sub
   
   Overloads Public Shared Function Main(args() As String) As Integer
      Dim writer As StreamWriter = Nothing
      Dim standardOutput As TextWriter = Console.Out
      
      If args.Length < 3 Then
         Console.WriteLine(usageText)
         Return 1
      End If
      
      Try
         writer = New StreamWriter(args(2))
         Console.SetOut(writer)
         Console.SetIn(New StreamReader(args(1)))
      Catch e As IOException
         Dim errorWriter As TextWriter = Console.Error
         errorWriter.WriteLine(e.Message)
         errorWriter.WriteLine(usageText)
         Return 1
      End Try
      Dim i As Integer
      i = Console.Read()
      While i <> -1 
         Dim c As Char = Convert.ToChar(i)
         If c = ControlChars.Tab Then
            Console.Write("".PadRight(tabSize, " "c))
         Else
            Console.Write(c)
         End If
         i = Console.Read()
      End While
      writer.Close()
      ' Recover the standard output stream so that a 
      ' completion message can be displayed.
      Console.SetOut(standardOutput)
      Console.WriteLine("EXPANDTABSEX has completed the processing of {0}.", args(0))
      Return 0
   End Function 'Main
End Class 'ExpandTabs

[C#] 
public class ExpandTabs {
    private const int tabSize = 4;
    private const string usageText = "Usage: EXPANDTABSEX inputfile.txt outputfile.txt";
    public static int Main(string[] args) {
        StreamWriter writer = null;
        TextWriter standardOutput = Console.Out;

        if (args.Length < 2) {
            Console.WriteLine(usageText);
            return 1;
        }

        try {
            writer = new StreamWriter(args[1]);
            Console.SetOut(writer);
            Console.SetIn(new StreamReader(args[0]));
        }
        catch(IOException e) {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;            
        }
        int i;
        while ((i = Console.Read()) != -1) {
            char c = (char)i;
            if (c == '\t')
                Console.Write(("").PadRight(tabSize, ' '));
            else
                Console.Write(c);
        }
        writer.Close();
        // Recover the standard output stream so that a 
        // completion message can be displayed.
        Console.SetOut(standardOutput);
        Console.WriteLine("EXPANDTABSEX has completed the processing of {0}.", args[0]);
        return 0;
    }
}

[C++] 
int main() {
   const int tabSize = 4;
   String* args[] = Environment::GetCommandLineArgs();
   String* usageText = S"Usage: EXPANDTABSEX inputfile.txt outputfile.txt";

   StreamWriter* writer = 0;
   TextWriter* standardOutput = Console::Out;

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

   try {
      writer = new StreamWriter(args[2]);
      Console::SetOut(writer);
      Console::SetIn(new StreamReader(args[1]));
   }
   catch(IOException* e) {
      TextWriter* errorWriter = Console::Error;
      errorWriter->WriteLine(e->Message);
      errorWriter->WriteLine(usageText);
      return 1;            
   }
   int i;
   while ((i = Console::Read()) != -1) {
      Char c = (Char)i;
      if (c == '\t')
         Console::Write((S"")->PadRight(tabSize, ' '));
      else
         Console::Write(c);
   }
   writer->Close();
   // Recover the standard output stream so that a 
   // completion message can be displayed.
   Console::SetOut(standardOutput);
   Console::WriteLine(S"EXPANDTABSEX has completed the processing of {0}.", args[0]);
   return 0;
}

[JScript] 
public class ExpandTabs {
    private static var tabSize : int = 4;
    private static var usageText : String  = "Usage: EXPANDTABSEX inputfile.txt outputfile.txt";
    public static function Main(args : String[]) : int {
        var writer : StreamWriter = null;
        var standardOutput : TextWriter = Console.Out;

        if (args.Length < 2) {
            Console.WriteLine(usageText);
            return 1;
        }

        try {
            writer = new StreamWriter(args[1]);
            Console.SetOut(writer);
            Console.SetIn(new StreamReader(args[0]));
        }
        catch(e : IOException) {
            var errorWriter : TextWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;            
        }
        var i : int;
        while ((i = Console.Read()) != -1) {
            var c : char = char(i);
            if (c == '\t')
                Console.Write(("").PadRight(tabSize, ' '));
            else
                Console.Write(c);
        }
        writer.Close();
        // Recover the standard output stream so that a 
        // completion message can be displayed.
        Console.SetOut(standardOutput);
        Console.WriteLine("EXPANDTABSEX has completed the processing of {0}.", args[0]);
        return 0;
    }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Console Class | Console Members | System Namespace | In | Out | SetError

Show:
© 2014 Microsoft