1. 程式人生 > >[C#]統計文本文件txt中的行數(快速讀取)

[C#]統計文本文件txt中的行數(快速讀取)

cnblogs () stop static nbsp nes req stream filename

快速統計文本文件中的行數( StreamReader.ReadLine() ):

測試代碼如下:

 1        //讀取txt文件中總行數的方法
 2         public static int requestMethod(String _fileName)
 3         {
 4             Stopwatch sw = new Stopwatch();
 5             var path = _fileName;
 6             int lines = 0;
 7 
 8             //按行讀取
 9             sw.Restart();
10 using (var sr = new StreamReader(path)) 11 { 12 var ls = ""; 13 while ((ls = sr.ReadLine()) != null) 14 { 15 lines++; 16 } 17 } 18 sw.Stop(); 19 return lines;
20 }

  

[C#]統計文本文件txt中的行數(快速讀取)