博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[小工具]代码统计小工具编写
阅读量:7061 次
发布时间:2019-06-28

本文共 4710 字,大约阅读时间需要 15 分钟。

代码小工具是有一定工作经验并且有更高追求的程序的必备技能,今天加班到早晨五六点,到家都七点了,回到家倒头就睡,下午来公司感觉头还有点晕乎,工作的话怕只会产生更多的代码,就想起来写个工程代码统计工具。

效果图

这里写图片描述

上图是平时积累的小工具,积累我认识也是程序员必备的素养!

主要代码

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 offSet BatDir=%~dp0set/p path=请输入你的工程路径:Set ToolPath=%BatDir%..\CodeCounter\CSProjectCodeLine.execall %ToolPath% %path%echo.=====检查完毕====

使用方法看上面效果图

工程下载

你可能感兴趣的文章
最近踩坑汇总
查看>>
骨牌(/棋子?)覆盖问题
查看>>
浅谈差分
查看>>
LEFT JOIN、RIGHT JOIN、INNER JOIN、FULL JOIN 使用
查看>>
谈谈 oc原生和跳转h5 页面 来回切换的功能
查看>>
【二维树状数组】【CF10D】 LCIS
查看>>
Hive基础知识
查看>>
解决windows系统80端口被占用问题
查看>>
redis基础和sentinel
查看>>
改变ubuntu终端显示语言(桌面系统是中文,终端提示是英文)
查看>>
13.scrapy框架的日志等级和请求传参
查看>>
linux 打开FTP 功能
查看>>
rollout
查看>>
上传图片前判断图片的尺寸
查看>>
dm8148 开发之---IDR帧
查看>>
电子自旋
查看>>
weka中算法说明[转]
查看>>
leetcode — plus-one
查看>>
less语法小结
查看>>
人人贷网的数据爬取
查看>>