c# Winform WPF 代码统计小程序的实现

      最近需要按照GJB编写研制总结报告,涉及到代码统计,之前是按照网上提到的正则表达式进行初步统计,再由人工进行详细统计。效率低下,通过查询后,发现自己实现好一些,用了2个小时,做了这个小demo。

       参考文章:https://blog.csdn.net/chr23899/article/details/43671281

                         https://www.cnblogs.com/fuyun2000/archive/2013/06/16/3138937.html

下面讲一下思路:

1、选定一个文件夹做为根目录,遍历该根目录下所有的文件夹及文件。

2、将需要统计代码的文件进行标红。

3、开始对标红的文件进行统计,除去引用,以及系统生成的代码,对注释和剩余的代码进行统计。

4、将统计结果导出成word表格,方便使用。

一、遍历、标红:

 private void LoadDirectory(string path, TreeNode nodeTemp)
        {
            DirectoryInfo info = new DirectoryInfo(path);

            //获取文件夹后缀名
            string str = info.Extension;
            //FullName:获取全路径
            str = info.FullName;
            TreeNode node = new TreeNode();
            if (info.Exists)
            {
                if (info.Attributes.ToString().Contains(FileAttributes.Hidden.ToString()))
                    return;
                node.Tag = info;
                node.Text = "文件夹:" + info.Name;
            }
            string[] fileList = Directory.GetFiles(path);
            for (int i = 0; i < fileList.Length; i++)
            {
                if (File.GetAttributes(fileList[i]).ToString().Contains(FileAttributes.Hidden.ToString()))
                    continue;
                TreeNode node1 = new TreeNode();
                node1.Tag = fileList[i];
                node1.Text = "文件:" + Path.GetFileName(fileList[i]);
                node.Nodes.Add(node1);

                List<string> extensionList = new List<string>();//扩展名列表,可自定义复选框扩充
                if (cbCSharp.Checked)
                    extensionList.Add(cbCSharp.Text);
                if (cbXaml.Checked)
                    extensionList.Add(cbXaml.Text);
                foreach (string extension in extensionList)
                {
                    if (extension.EndsWith(Path.GetExtension(fileList[i])))
                    {
                        analysisFileList.Add(fileList[i]);
                        node1.ForeColor = Color.Red;
                    }
                }
            }
            node.Expand();
            if (nodeTemp == null)
                MyTreeView.Nodes.Add(node);
            else
                nodeTemp.Nodes.Add(node);
            //GetDirectories:获取指定目录中子目录的名称
            string[] directoryList = Directory.GetDirectories(path);
            for (int i = 0; i < directoryList.Length; i++)
            {
                LoadDirectory(directoryList[i], node);//递归获取所有文件夹下所有文件
            }
        }

二、统计

  private void btnStart_Click(object sender, EventArgs e)
        {
            int codeCount = 0;//代码行数
            int commentCount = 0;//注释行数
            foreach (string file in analysisFileList)
            {
                FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);

                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine();
                    str = str.Trim();
                    if (str == "" || str.StartsWith("using") || str.StartsWith("namespace"))
                        continue;
                    if (str.StartsWith("//"))//这里还需要考虑/* */注释的处理方式
                    {
                        commentCount++;
                        continue;
                    }
                    else
                    {
                        codeCount++;
                        if (str.Contains("//"))
                            commentCount++;
                    }

                }
                sr.Close();
                fs.Close();

                StatisticsInfo info = new StatisticsInfo();
                info.fileName = Path.GetFileName(file);
                info.codeCount = codeCount;
                info.commentCount = commentCount;
                infoList.Add(info);

                txtAnalysis.Text += info.fileName + "  " + codeCount.ToString() + "    " + commentCount.ToString() + Environment.NewLine;
            }
        }

三、导出word

  private void btnExportWord_Click(object sender, EventArgs e)
        {

            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            try
            {

                int rows = infoList.Count() + 1;//表格行数加1是为了标题栏
                int cols = 3;//表格列数
                object oMissing = System.Reflection.Missing.Value;
                app = new Microsoft.Office.Interop.Word.Application();//创建word应用程序
                doc = app.Documents.Add();//添加一个word文档

                //换行添加表格
                Microsoft.Office.Interop.Word.Range range = app.Selection.Range;
                Microsoft.Office.Interop.Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);

                //设置表格的字体大小粗细
                table.Range.Font.Size = 10;
                table.Range.Font.Bold = 0;

                //设置表格标题
                int rowIndex = 1;
                table.Cell(rowIndex, 1).Range.Text = "文件名";
                table.Cell(rowIndex, 2).Range.Text = "代码数";
                table.Cell(rowIndex, 3).Range.Text = "注释数";


                foreach (var i in infoList)
                {
                    //循环数据创建数据行
                    rowIndex++;
                    table.Cell(rowIndex, 1).Range.Text = i.fileName;//文件名
                    table.Cell(rowIndex, 2).Range.Text=i.codeCount.ToString();//代码数
                    table.Cell(rowIndex, 3).Range.Text = i.commentCount.ToString();//注释数

                    //对表格中单元格设置上下居中
                    table.Cell(rowIndex, 1).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 2).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 3).VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                }

                //导出到文件
                string newFile = System.Windows.Forms.Application.StartupPath+@"/" +DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
                doc.SaveAs(newFile,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
                Process.Start(newFile);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//关闭文档
                }
                if (app != null)
                {
                    app.Quit();//退出应用程序
                }
            }
        }

四、效果图

主界面:

word表格效果

五、需要完善文件类型以及代码的统计处理方式,目前的统计方式过于简单粗暴,还不能做到精确处理。

六、如想自行完善源码,或直接使用,可在csdn搜索资源:https://download.csdn.net/download/lefuture2/11811599

        

 

因为一直想找个办法来统计一下自己写的代码总行数,但是打开代码文件一个一个的加有点儿太二了.而且还不能忽略空行和注释.因此就写了这么程序用来统计真实的代码总行数. 如果各位有什么高见一定要给在下指教 本程序支持对sln(解决方案文件)、csproj(c#项目文件)、vbproj(vb.net项目文件)下所包含的源代码进行代码行数统计工作. 支持vs2003\2005\2008等版本所生成的解决方案文件或者项目文件 程序针对的语言为.net winform下的C#和VB.net,不支持J#.c++尚未测试 在计算代码行数时可以选择忽略空行、忽略注释或者忽略visual studio所自动生成的文件。这样可以得到自己亲手写的真实的代码行数。 同时,显示窗里可以看到该 解决方案文件或者项目文件下所包含的文件列表(有图表显示)界面还算漂亮 该树状列表可以支持多选,你可以只选择其中指定的几个文件或者项目进行代码行数统计,还是比较方便的。 v2.3.5版 1.支持C#与vb.net混合编程生成的解决方案文件,通过该解决方案文件可以统计解决方案下所有的代码文件的代码行数下载地址 2.增加C#与VB.NET的图标,可以更加清楚的识别不同语言写出的代码文件. 3.增加鼠标悬停提示,不必最大化程序或者或者拉伸标题栏就,只需悬停鼠标在指定的节点上就可显示该文件的完整名称. 4.增加"展开所有节点"选项框,可以让程序自动展开所有节点. 可以看我的博客,详细介绍发在这里了:http://www.cnblogs.com/tannaiyin/archive/2009/06/04/1496438.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值