Console.OpenStandardError 方法
定义
获取标准错误流。Acquires the standard error stream.
重载
| OpenStandardError() |
获取标准错误流。Acquires the standard error stream. |
| OpenStandardError(Int32) |
获取设置为指定缓冲区大小的标准错误流。Acquires the standard error stream, which is set to a specified buffer size. |
OpenStandardError()
获取标准错误流。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
返回
标准错误流。The standard error stream.
示例
下面的示例是一个简单的文本文件查看器,可在控制台中显示一个或多个文本文件的内容。The following example is a simple text file viewer that displays the contents of one or more text files to the console. 如果没有命令行参数,或者作为命令行参数传递的任何文件都不存在,则此示例将调用 SetError 方法将错误信息重定向到文件, OpenStandardError 在 reacquiring 标准错误流的过程中调用方法,并指示错误信息已写入文件。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.
请注意,在 StreamWriter.AutoFlush true reacquiring 错误流之前,属性设置为。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.
注解
此方法可用于在方法更改标准错误流后重新获取该流 SetError 。This method can be used to reacquire the standard error stream after it has been changed by the SetError method.
另请参阅
适用于
OpenStandardError(Int32)
获取设置为指定缓冲区大小的标准错误流。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
参数
- bufferSize
- Int32
此参数无效,但其值必须大于或等于零。This parameter has no effect, but its value must be greater than or equal to zero.
返回
标准错误流。The standard error stream.
例外
bufferSize 小于或等于零。bufferSize is less than or equal to zero.
注解
此方法可用于在方法更改标准错误流后重新获取该流 SetError 。This method can be used to reacquire the standard error stream after it has been changed by the SetError method.