本文整理汇总了C#中System.Console.Out属性的典型用法代码示例。如果您正苦于以下问题:C# Console.Out属性的具体用法?C# Console.Out怎么用?C# Console.Out使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Console
的用法示例。
在下文中一共展示了Console.Out属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
public class Example
{
public static void Main()
{
// Get all files in the current directory.
string[] files = Directory.GetFiles(".");
Array.Sort(files);
// Display the files to the current output source to the console.
Console.Out.WriteLine("First display of filenames to the console:");
Array.ForEach(files, s => Console.Out.WriteLine(s));
Console.Out.WriteLine();
// Redirect output to a file named Files.txt and write file list.
StreamWriter sw = new StreamWriter(@".\Files.txt");
sw.AutoFlush = true;
Console.SetOut(sw);
Console.Out.WriteLine("Display filenames to a file:");
Array.ForEach(files, s => Console.Out.WriteLine(s));
Console.Out.WriteLine();
// Close previous output stream and redirect output to standard output.
Console.Out.Close();
sw = new StreamWriter(Console.OpenStandardOutput());
sw.AutoFlush = true;
Console.SetOut(sw);
// Display the files to the current output source to the console.
Console.Out.WriteLine("Second display of filenames to the console:");
Array.ForEach(files, s => Console.Out.WriteLine(s));
}
}
示例2: Main
//引入命名空间
using System;
class MainClass {
public static void Main() {
int a=10, b=0;
int result;
Console.Out.WriteLine("This will generate an exception.");
try {
result = a / b; // generate an exception
} catch(DivideByZeroException exc) {
Console.Error.WriteLine(exc.Message);
}
}
}