
.NET ez 2012-05-04
以往讀取檔案使用 ReadAllLines,需要一次載入所有內容,所以會有卡住的情況發生。
foreach (string line in File.ReadAllLines(@"C:\a.log")) { Console.WriteLine(line); }
為了要改善,所以改寫為以下,一次讀取一行:
using (StreamReader SR = new StreamReader(@"C:\a.log")) { string line; while ((line = SR.ReadLine()) != null) { Console.WriteLine(line); } }
.Net 4.0之後可以使用ReadLines,即可達成以上效果。
foreach (string line in File.ReadLines(@"C:\a.log")) { Console.WriteLine(line); }
標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-ReadAllLines-ReadLines
https://www.ez2o.com/Blog/Post/111
https://www.ez2o.com/Blog/Post/csharp-ReadAllLines-ReadLines
https://www.ez2o.com/Blog/Post/111