如果是文本文件,您可以使用以下File.ReadAllLines方法:
string[] lines = File.ReadAllLines("myfile.pop");
Run Code Online (Sandbox Code Playgroud)
然后你可以循环遍历行数组或通过索引访问各行.但请注意,这会将整个文件内容加载到内存中,如果文件非常大,则可能不是很好.在这种情况下,您可以使用以下方法逐行阅读StreamReader:
using (var stream = File.OpenRead("myfile.pop"))
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
// do something with the line here
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,只有一行加载到内存中.