先在App.config中追加2个AppSettings,用来保存文件所在目录和文件扩展名。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key="FilePath" value="D:\SVN" /> <add key="FileExtention" value="*.cs" /> </appSettings> </configuration>
统计行数代码:Program.cs
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(); } } }
原文地址:https://www.cnblogs.com/wyvern0618/p/9704971.html
时间: 2024-11-08 19:43:33