统计代码文件的注释行
2014-03-02 00:24
204 查看
涉及了并行处理、lambda表达式、LINQ、字典集合、using语句、文件处理、匿名类型等知识点。
功能:统计指定目录下(包括子目录)所有.CS文件中的以\\开头的注释行
源码:
稍微试了下:
---------------
搜索总文件数:750,总行数:593673,总耗时0.8110464秒
功能:统计指定目录下(包括子目录)所有.CS文件中的以\\开头的注释行
源码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Cryking { class Program { static void Main(string[] args) { Console.WriteLine("请输入工程所在的目录:"); string strPath=Console.ReadLine(); string[] astrFile = Directory.GetFiles(strPath,"*.cs",SearchOption.AllDirectories); var files = from f in astrFile //LINQ where !f.ToLower().Contains(".designer.cs") select f; Dictionary<string, int[]> dic = new Dictionary<string, int[]>(); DateTime dt = DateTime.Now; Parallel.ForEach(files, strFile =>//并行处理+lambda表达式 { Console.WriteLine("正在搜索文件:{0}", strFile); using (StreamReader sr = new StreamReader(strFile, Encoding.Default)) { string s; int[] Count = new int[4] { 0, 0, 0, 0 }; int conCount = 0; bool flag = false;//连续注释标志 while (null != (s = sr.ReadLine())) { if (s.Trim().Equals("")) Count[3]++; else if (s.Trim().Length >= 2 && s.Trim().Substring(0, 2) == @"//") { if (flag) { conCount++; Count[1] = Math.Max(conCount, Count[1]); } else conCount = 0; Count[2]++; flag = true; } else flag = false; Count[0]++; } dic.Add(strFile, Count); } }); StreamWriter sw = new StreamWriter("SearchComm.txt",true, Encoding.Default); long sumLines = 0; foreach (KeyValuePair<string, int[]> kvp in dic) { sw.WriteLine("file={0} 行数={1}, 最大连续注释行数={2}, 总注释行数={3}, 空白行数={4}", kvp.Key, kvp.Value[0], kvp.Value[1], kvp.Value[2], kvp.Value[3]); sumLines += kvp.Value[0]; } sw.WriteLine("{0}","-".PadLeft(15,'-')); string sumTime=(DateTime.Now - dt).TotalSeconds.ToString(); sw.WriteLine("搜索总文件数:{0},总行数:{1},总耗时{2}秒", files.Count(), sumLines, sumTime); sw.Close(); Console.WriteLine("搜索完成!\n搜索总文件数:{0},共耗时{1}秒.",files.Count(), sumTime); Console.ReadKey(); } } }
稍微试了下:
---------------
搜索总文件数:750,总行数:593673,总耗时0.8110464秒
相关文章推荐
- java简单统计.java文件中的有效代码行,空行,注释行
- java正则表达式例程 提取网页中的email 统计代码的空行 有效代码行 注释行
- Linux统计文件个数或是代码行数
- Atitit.Guibutton与面板---项目规模的评估----文件数统计,结构,代码行数,每类型文件行数.
- 利用正则表达式统计某个某个文件夹下所有java文件的 空格行数、注释行数、和代码行数
- 统计文件的代码行数
- 利用Clojure统计代码文件数量和代码行数
- 用代码统计代码文件(python)
- Python统计python文件中代码,注释及空白对应的行数示例【测试可用】
- 【linux命令】代码或文件统计行数
- 小脚本,统计一个目录下满足特定条件文件的代码行数
- C语言读取文件内容统计代码
- linux下如何统计一个目录下的文件个数以及代码总行数的命令
- 统计文件夹内.c .cpp .py类型文件的代码行数
- 一个用shell写的统计目录下统计文件行数的代码
- 统计某java文件中的代码行数、某个字符(char)、某字符串(String)出现的次数
- 读取文件的目录结构和统计文件的代码信息
- 统计工程的文件数,整体代码的行数
- Linux下控制(统计)文件的生成的C代码实现
- 控制台——代码行数统计、删除.svn文件、静态库命令、protocBuf命令