[C#]统计文本文件txt中的行数(快速读取)

快速统计文本文件中的行数( 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         }

 

  

转载于:https://www.cnblogs.com/ttkl/p/7640199.html

要在C#读取文本文件行数,可以使用以下代码: ```csharp using System; using System.IO; class Program { static void Main(string\[\] args) { string filePath = "L:\\Code\\1.txt"; // 文件路径 int lineCount = GetLineCount(filePath); // 调用方法获取行数 Console.WriteLine("该文件一共有" + lineCount + ""); Console.ReadKey(); } public static int GetLineCount(string filePath) { int lineCount = 0; using (StreamReader sr = new StreamReader(filePath)) { while (sr.ReadLine() != null) { lineCount++; } } return lineCount; } } ``` 以上代码,`GetLineCount`方法接收一个文件路径作为参数,使用`StreamReader`读取文件的每一,并通过计数器统计行数。最后返回行数。在`Main`方法,我们调用`GetLineCount`方法并输出行数。请注意,你需要将文件路径`filePath`替换为你实际的文件路径。\[1\] #### 引用[.reference_title] - *1* [C#读取txt文本内容行数](https://blog.csdn.net/qq_22889875/article/details/77924840)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C#语言读取txt列数据](https://blog.csdn.net/chengoes/article/details/121409682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [通过程序 VB.Net 或 C# 读取文本文件行数](https://blog.csdn.net/weixin_34218890/article/details/86309622)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值