【发布时间】:2021-09-15 08:46:13
【问题描述】:
我知道在foreach 循环中使用File.ReadLines() 返回的IEnumerable 时,文件会在循环后自动关闭。
我只需要快速检查文件的第一行。这足够吗?还是会让文件保持打开状态?
protected void Append(string filePath, Encoding encoding)
{
try
{
string firstLine = File.ReadLines(filePath, encoding).First();
// more code here
}
catch
{
// more code here
}
}
【问题讨论】:
-
@paulsm4 是
File.ReadAllLines(),但File.ReadLines()确实保持文件打开直到迭代器被释放。
标签: c# .net file enumerable