用C#编写统计文本行数的代码
2021-07-09 16:05
标签:目录 class ons 编写 names name sys style dir 先在App.config中追加2个AppSettings,用来保存文件所在目录和文件扩展名。 统计行数代码:Program.cs 用C#编写统计文本行数的代码 标签:目录 class ons 编写 names name sys style dir 原文地址:https://www.cnblogs.com/wyvern0618/p/9704971.html"1.0" encoding="utf-8" ?>
using System;
using System.IO;
namespace ConsoleApp13
{
class Program
{
static void Main(string[] args)
{
int fileCount = 0;
int lines = 0;
string folder = System.Configuration.ConfigurationManager.AppSettings["FilePath"] ;
string extention = System.Configuration.ConfigurationManager.AppSettings["FileExtention"];
foreach (var path in Directory.EnumerateFiles(folder, extention, SearchOption.AllDirectories))
{
fileCount++;
using (var sr = new StreamReader(path))
{
int line = 0;
var ls = "";
while ((ls = sr.ReadLine()) != null)
{
line++;
}
Console.WriteLine(Path.GetFileName(path) +":"+ line);
lines += line;
}
}
Console.WriteLine("fileCount=" + fileCount);
Console.WriteLine("lines=" + lines);
Console.ReadLine();
}
}
}
上一篇:WPF 实现波浪浮动效果