[小工具]代码统计小工具编写

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

效果图

这里写图片描述

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

主要代码

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;

        /// <summary>
        /// 代码行数
        /// </summary>
        public int CodeLines
        {
            get
            {
                return codeLines;
            }

            set
            {
                codeLines = value;
            }
        }

        private int commentLines;

        /// <summary>
        /// 注释行数
        /// </summary>
        public int CommentLines
        {
            get
            {
                return commentLines;
            }

            set
            {
                commentLines = value;
            }
        }

        private int fileNumber;

        /// <summary>
        /// 文件个数
        /// </summary>
        public int FileNumber
        {
            get
            {
                return fileNumber;
            }

            set
            {
                fileNumber = value;
            }
        }

        public CodeCounter()
        {
            codeLines = 0;
            commentLines = 0;
            fileNumber = 0;
        }

        /// <summary>
        /// 获取整个项目的代码统计情况
        /// </summary>
        /// <param name="folderPath"></param>
        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);
            }
        }

        /// <summary>
        /// 单个文件代码统计情况
        /// </summary>
        /// <param name="fileName"></param>
        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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值