我正在从命令行读取字符串。但我的程序无法检测到流的结尾。我如何重建这个或有没有办法明确地将EndOfStream设置为true?
List<String> str = new List<String>();
using (StreamReader reader = new StreamReader(Console.OpenStandardInput()))
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line != string.Empty)
{
str.Add(line);
}
}
这无法通过设计解决。只要程序处于活动状态,就会打开stdin / stdout控制台流。在这种情况下关闭应用程序之前执行EndOfStream
。
您的问题的一个很好的解决方案是。
using System;
public class Example
{
public static void Main()
{
string line;
do {
line = Console.ReadLine();
if (line != null)
Console.WriteLine("Now I have detected the end of stream.... " + line);
} while (line != null);
}
}