首页 > Windows开发 > 详细

用C#编写统计文本行数的代码

时间:2018-09-26 10:02:54      阅读:225      评论:0      收藏:0      [点我收藏+]

先在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();
        }

    }
}

 

用C#编写统计文本行数的代码

原文:https://www.cnblogs.com/wyvern0618/p/9704971.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!