本文整理汇总了C#中System.Console.Error属性的典型用法代码示例。如果您正苦于以下问题:C# Console.Error属性的具体用法?C# Console.Error怎么用?C# Console.Error使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Console
的用法示例。
在下文中一共展示了Console.Error属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
public class ExpandTabs
{
private const int tabSize = 4;
private const string usageText = "Usage: EXPANDTABSEX inputfile.txt outputfile.txt";
public static void Main(string[] args)
{
StreamWriter writer = null;
if (args.Length < 2) {
Console.WriteLine(usageText);
return;
}
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;
}
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.
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("EXPANDTABSEX has completed the processing of {0}.", args[0]);
return;
}
}
示例2: Main
//引入命名空间
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.
示例3: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
int increment = 0;
bool exitFlag = false;
while (! exitFlag) {
if (Console.IsOutputRedirected)
Console.Error.WriteLine("Generating multiples of numbers from {0} to {1}",
increment + 1, increment + 10);
Console.WriteLine("Generating multiples of numbers from {0} to {1}",
increment + 1, increment + 10);
for (int ctr = increment + 1; ctr <= increment + 10; ctr++) {
Console.Write("Multiples of {0}: ", ctr);
for (int ctr2 = 1; ctr2 <= 10; ctr2++)
Console.Write("{0}{1}", ctr * ctr2, ctr2 == 10 ? "" : ", ");
Console.WriteLine();
}
Console.WriteLine();
increment += 10;
Console.Error.Write("Display multiples of {0} through {1} (y/n)? ",
increment + 1, increment + 10);
Char response = Console.ReadKey(true).KeyChar;
Console.Error.WriteLine(response);
if (! Console.IsOutputRedirected)
Console.CursorTop--;
if (Char.ToUpperInvariant(response) == 'N')
exitFlag = true;
}
}
}