我在c#中逐行阅读一个简单的文本文件,但是我得到了这个错误,我无法理解。
这是我的简单代码:
StreamReader reader = new StreamReader("hello.txt");
但这给了我一个错误:
参数1:无法从“字符串”转换为“System.IO.Stream”
本文是关于msdn的使用相同的代码,在那里工作,我做错了什么?
发布于 2017-09-25 12:21:57
如果你想读一个文件,最简单的方法是
var path = "c:\\mypath\\to\\my\\file.txt";
var lines = File.ReadAllLines(path);
foreach (var line in lines)
{
Console.WriteLine(line);
}
你也可以这样做:
var path = "c:\\mypath\\to\\my\\file.txt";
using (var reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
}
发布于 2017-09-25 12:23:01
你可以这样做
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\\hello.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
https://stackoverflow.com/questions/46405067
复制相似问题