# CodeCounter **Repository Path**: dingxiaowei/CodeCounter ## Basic Information - **Project Name**: CodeCounter - **Description**: C#代码统计 - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2017-11-01 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # C#代码统计工具 #### 代码小工具是有一定工作经验并且有更高追求的程序的必备技能,今天加班到早晨五六点,到家都七点了,回到家倒头就睡,下午来公司感觉头还有点晕乎,工作的话怕只会产生更多的代码,就想起来写个工程代码统计工具。 ### 效果图 ![这里写图片描述](http://img.blog.csdn.net/20171101185132713?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGluZ3hpYW93ZWkyMDEz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast) 上图是平时积累的小工具,积累我认识也是程序员必备的素养! ### 主要代码 ``` using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace CSProjectCodeLine { class Program { static int Main(string[] args) { int result = 0; try { Console.WriteLine("===============Aladdin Tool================"); Console.WriteLine("参数:打表配置文件名或打表配置文件所在的目录"); Console.WriteLine("==========================================="); if (args.Length == 0) { Console.WriteLine("Error:请填写路径参数"); Console.ReadKey(); result = 1; } else if (args.Length == 1) { DirectoryInfo dirInfo = new DirectoryInfo(args[0]); if (dirInfo == null) { Console.WriteLine("Error:路径有误"); Console.ReadKey(); result = 1; } else { CodeCounter counter = new CodeCounter(); counter.GetProjectResult(dirInfo.FullName); int codenumber = counter.CodeLines; int filenumber = counter.FileNumber; Console.WriteLine("项目中共有cs代码文件" + filenumber + "个"); Console.WriteLine("项目中共有代码" + codenumber + "行"); Console.ReadKey(); result = 0; } } else { Console.WriteLine("Error:参数只支持一个工程路径"); Console.ReadKey(); result = 1; } } catch (Exception ex) { Console.ForegroundColor = ((Console.ForegroundColor == ConsoleColor.Red) ? ConsoleColor.Green : ConsoleColor.Red); Console.WriteLine(ex.ToString()); Console.ResetColor(); result = 1; } return result; } } } ``` ``` using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace CSProjectCodeLine { class CodeCounter { private int codeLines; /// /// 代码行数 /// public int CodeLines { get { return codeLines; } set { codeLines = value; } } private int commentLines; /// /// 注释行数 /// public int CommentLines { get { return commentLines; } set { commentLines = value; } } private int fileNumber; /// /// 文件个数 /// public int FileNumber { get { return fileNumber; } set { fileNumber = value; } } public CodeCounter() { codeLines = 0; commentLines = 0; fileNumber = 0; } /// /// 获取整个项目的代码统计情况 /// /// public void GetProjectResult(string folderPath) { DirectoryInfo folder = new DirectoryInfo(folderPath); try { var folders = folder.GetDirectories(); for (int i = 0; i < folders.Length; i++) { //递归调用 有时候会碰到目录权限问题 GetProjectResult(folders[i].FullName); } var csFiles = folder.GetFiles("*.cs"); for (int i = 0; i < csFiles.Length; i++) { fileNumber++; string fieleName = folderPath + "\\" + csFiles[i].Name; GetCodeFileResult(fieleName); } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 单个文件代码统计情况 /// /// public void GetCodeFileResult(string fileName) { string line; StreamReader sr = new StreamReader(fileName); while ((line = sr.ReadLine()) != null) { codeLines++; } } } } ``` ### 批处理 ``` @echo off Set BatDir=%~dp0 set/p path=请输入你的工程路径: Set ToolPath=%BatDir%..\CodeCounter\CSProjectCodeLine.exe call %ToolPath% %path% echo.=====检查完毕==== ``` 使用方法看上面效果图 ### 工程下载 https://gitee.com/dingxiaowei/CodeCounter/tree/master/CSProjectCodeLine/CSProjectCodeLine ### 原文链接 http://blog.csdn.net/dingxiaowei2013/article/details/78417273