如何从TextReader source循环行
我试过了
foreach (var line in source)但得到了错误
foreach语句不能对“System.IO.TextReader”类型的变量进行操作,因为“System.IO.TextReader”不包含“GetEnumerator”的公共定义
发布于 2012-10-02 09:13:47
string line;
while ((line = myTextReader.ReadLine()) != null)
{
DoSomethingWith(line);
}发布于 2012-10-02 09:10:47
您可以使用File.ReadLines,它是延迟执行方法,然后通过行循环:
foreach (var line in File.ReadLines("test.txt"))
{
}更多信息:
http://msdn.microsoft.com/en-us/library/dd383503.aspx
发布于 2012-10-02 09:10:56
您可以尝试使用基于ReadLine method的代码。
string line = null;
System.IO.TextReader readFile = new StreamReader("...."); //Adjust your path
while (true)
{
line = readFile.ReadLine();
if (line == null)
{
break;
}
MessageBox.Show (line);
}
readFile.Close();
readFile = null;https://stackoverflow.com/questions/12687453
复制相似问题