visual-studio 如何计算 Visual Studio 解决方案中的代码行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1244729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:50:36  来源:igfitidea点击:

How do you count the lines of code in a Visual Studio solution?

visual-studiocode-metricslines-of-codeline-count

提问by Fermin

Is it possible to find the number of lines of code in an entire solution? I've heard of MZ-Tools, but is there an open source equivalent?

是否可以在整个解决方案中找到代码行数?我听说过MZ-Tools,但是否有开源等效项?

采纳答案by Herter

Visual Studio 2010 Ultimate has this built-in:

Visual Studio 2010 Ultimate 具有以下内置功能:

Analyze → Calculate Code Metrics

分析 → 计算代码指标

回答by Greg D

I've found powershelluseful for this. I consider LoC to be a pretty bogus metric anyway, so I don't believe anything more formal should be required.

我发现powershell对此很有用。无论如何,我认为 LoC 是一个非常虚假的指标,所以我认为不需要任何更正式的东西。

From a smallish solution's directory:

从一个小的解决方案的目录:

PS C:\Path> (gci -include *.cs,*.xaml -recurse | select-string .).Count
8396
PS C:\Path>

That will count the non-blank lines in all the solution's .cs and .xaml files. For a larger project, I just used a different extension list:

这将计算所有解决方案的 .cs 和 .xaml 文件中的非空行。对于更大的项目,我只是使用了不同的扩展列表:

PS C:\Other> (gci -include *.cs,*.cpp,*.h,*.idl,*.asmx -recurse | select-string .).Count
909402
PS C:\Other>

Why use an entire app when a single command-line will do it? :)

当一个命令行就可以完成时,为什么要使用整个应用程序?:)

回答by NicolásKittsteiner

I used Ctrl+Shift+F. Next, put a \nin the search box and enable regular expressions box. Then in the find results, in the end of the screen are the number of files searched and lines of code found.

我用Ctrl+ Shift+ F。接下来,\n在搜索框中输入一个并启用正则表达式框。然后在查找结果中,屏幕末尾是搜索到的文件数和找到的代码行数。

You can use [^\n\s]\r\nto skip blank and space-only lines (credits to Zach in the comments).

您可以使用[^\n\s]\r\n跳过空白行和仅空格行(感谢 Zach 在评论中)。

回答by Ali Parr

An open source line counter for VS2005, 2003 and 2002 is available here:

VS2005、2003 和 2002 的开源行计数器可在此处获得:

http://www.wndtabs.com/

http://www.wndtabs.com/

There is also discussion of creating a line counting VS addin, complete with code on Codeproject, here

还有关于创建行计数VS插件的讨论,在Codeproject上完成代码,这里

http://www.codeproject.com/KB/macros/LineCounterAddin.aspx

http://www.codeproject.com/KB/macros/LineCounterAddin.aspx

Also Slick Edit Gadgets have a nice line-counter, here:

Slick Edit Gadgets 也有一个不错的行计数器,在这里:

http://www.slickedit.com/products/slickedit

http://www.slickedit.com/products/slickedit

and Microsoft Visual Studio Team System 2008 includes a good line counter.

和 Microsoft Visual Studio Team System 2008 包括一个很好的行计数器。

Just remember though:

不过请记住:

Measuring programming progress by lines of code is like measuring aircraft building progress by weight. Bill Gates

用代码行衡量编程进度就像用重量衡量飞机制造进度。比尔盖茨

回答by Howard Renollet

Here's an update for Visual Studio 2012/2013/2015 for those who want to do the "Find" option (which I find to be the easiest): This RegEx will find all non-blank lines with several exclusions to give the most accurate results.

这是 Visual Studio 2012/2013/2015 的更新,适用于那些想要执行“查找”选项(我认为这是最简单的)的人:此 RegEx 将找到所有非空行,并带有多个排除项,以提供最准确的结果.

Enter the following RegEx into the "Find" box. Please make sure to select the "Use Regular Expressions" option. Change the search option to either "Current Project" or "Entire Solution" depending on your needs. Now select "Find All". At the bottom of the Find Resultswindow, you will see "Matching Lines" which is the lines of code count.

在“查找”框中输入以下正则表达式。请确保选择“使用正则表达式”选项。根据您的需要,将搜索选项更改为“当前项目”或“整个解决方案”。现在选择“查找全部”。在Find Results窗口的底部,您将看到“Matching Lines”,即代码行数。



^(?!(\s*\*))(?!(\s*\-\-\>))(?!(\s*\<\!\-\-))(?!(\s*\n))(?!(\s*\*\/))(?!(\s*\/\*))(?!(\s*\/\/\/))(?!(\s*\/\/))(?!(\s*\}))(?!(\s*\{))(?!(\s(using))).*$


This RegEx excludes the following items:

本 RegEx 不包括以下项目:



Comments

评论

// This is a comment


Multi-Line comments (assuming the lines are correctly commented with a * in front of each line)

多行注释(假设每行前面都用 * 正确注释了这些行)

/* I am a
* multi-line
* comment */


XML for Intellisense

用于智能感知的 XML

/// <summary>
/// I'm a class description for Intellisense
/// </summary>


HTML Comments:

HTML 注释:

<!-- I am a HTML Comment -->


Using statements:

使用语句:

using System;
using System.Web;


Opening curly braces:

开大括号:

{


Closing curly braces:

关闭花括号:

}


Note: anything between the braces would be included in the search, but in this example only 4 lines of code would count, instead of 18 actual non-blank lines:

注意:大括号之间的任何内容都将包含在搜索中,但在此示例中,仅计算 4 行代码,而不是实际的 18 行非空行:

        public class Test
        {
            /// <summary>
            /// Do Stuff
            /// </summary>
            public Test()
            {
                TestMe();
            }
            public void TestMe()
            {
                //Do Stuff Here
                /* And
                 * Do
                 * Stuff
                 * Here */
            }
        }


I created this to give me a much more accurate LOC count than some previous options, and figured I would share. The bosses love LOC counts, so I'm stuck with it for a while. I hope someone else can find this helpful, let me know if you have any questions or need help getting it to work.

我创建这个是为了给我一个比以前的一些选项更准确的 LOC 计数,并认为我会分享。老板们喜欢 LOC 计数,所以我坚持了一段时间。我希望其他人能发现这有帮助,如果您有任何问题或需要帮助使其正常工作,请告诉我。

回答by Mads Andersen

Found this tip: LOC with VS Find and replace

发现这个技巧: LOC with VS Find and replace

Not a plugin though if thats what you are looking for.

不是插件,但如果那是您正在寻找的。

回答by Mark Rushakoff

clocis an excellent commandline, Perl-based, Windows-executable which will break down the blank lines, commented lines, and source lines of code, grouped by file-formats.

cloc是一个出色的命令行,基于 Perl,Windows 可执行文件,它将分解空白行、注释行和代码源代码行,并按文件格式分组。

Now it won't specifically run on a VS solution file, but it can recurse through directories, and you can set up filename filters as you see fit.

现在它不会专门在 VS 解决方案文件上运行,但它可以遍历目录,并且您可以根据需要设置文件名过滤器。

Here's the sample output from their web page:

这是他们网页的示例输出:

prompt> cloc perl-5.10.0.tar.gz
    4076 text files.
    3883 unique files.                                          
    1521 files ignored.

http://cloc.sourceforge.net v 1.07  T=10.0 s (251.0 files/s, 84566.5 lines/s)
-------------------------------------------------------------------------------
Language          files     blank   comment      code    scale   3rd gen. equiv
-------------------------------------------------------------------------------
Perl               2052    110356    112521    309778 x   4.00 =     1239112.00
C                   135     18718     22862    140483 x   0.77 =      108171.91
C/C++ Header        147      7650     12093     44042 x   1.00 =       44042.00
Bourne Shell        116      3402      5789     36882 x   3.81 =      140520.42
Lisp                  1       684      2242      7515 x   1.25 =        9393.75
make                  7       498       473      2044 x   2.50 =        5110.00
C++                  10       312       277      2000 x   1.51 =        3020.00
XML                  26       231         0      1972 x   1.90 =        3746.80
yacc                  2       128        97      1549 x   1.51 =        2338.99
YAML                  2         2         0       489 x   0.90 =         440.10
DOS Batch            11        85        50       322 x   0.63 =         202.86
HTML                  1        19         2        98 x   1.90 =         186.20
-------------------------------------------------------------------------------
SUM:               2510    142085    156406    547174 x   2.84 =     1556285.03
-------------------------------------------------------------------------------

The third generation equivalent scale is a rough estimate of how much code it would take in a third generation language. Not terribly useful, but interesting anyway.

第三代等效规模是对第三代语言需要多少代码的粗略估计。不是非常有用,但无论如何都很有趣。

回答by Regfor

Answers here are a little bit out of date, may be from vs 2008 time. Because in newer Visual Studio versions 2010/2012, this feature is already built-in. Thus there are no reason to use any extension or tools for it.

这里的答案有点过时,可能是从 2008 年开始的。因为在较新的 Visual Studio 版本 2010/2012 中,此功能已内置。因此没有理由为此使用任何扩展或工具。

Feature to count lines of code - Calculate Metrics. With it you can calculate your metrics (LOC, Maintaince index, Cyclomatic index, Depth of inheritence) for each project or solution.

计算代码行数的功能 - 计算指标。有了它,您可以计算每个项目或解决方案的指标(LOC、维护指数、圈指数、继承深度)。

Just right click on solution or project in Solution Explorer,

只需右键单击解决方案资源管理器中的解决方案或项目,

enter image description here

在此处输入图片说明

and select "Calculate metrics"

并选择“计算指标”

enter image description here

在此处输入图片说明

Later data for analysis and aggregation could be imported to Excel. Also in Excel you can filter out generated classes, or other noise from your metrics. These metrics including Lines of code LOC could be gathered also during build process, and included in build report

以后用于分析和聚合的数据可以导入到 Excel 中。同样在 Excel 中,您可以从指标中过滤掉生成的类或其他干扰。这些指标包括代码行 LOC 也可以在构建过程中收集,并包含在构建报告中

回答by user2711915

Regular expressions have changed between VS2010 and 2012, so most of the regular expression solutions here no longer work

VS2010 和 2012 之间的正则表达式发生了变化,所以这里的大多数正则表达式解决方案不再有效

(^(?!(\s*//.+)))+(^(?!(#.+)))+(^(?!(\s*\{.+)))+(^(?!(\s*\}.+)))+(^(?!(\s*\r?$)))+

Will find all lines that are not blank, are not just a single bracket ( '{' or '}' ) and not just a #include or other preprocessor.

将找到所有非空白行,不仅仅是一个括号( '{' 或 '}' ),而不仅仅是一个 #include 或其他预处理器。

Use Ctrl-shift-fand make sure regular expressions are enabled.

使用Ctrl- shift-f并确保启用了正则表达式。

The corresponding regular expression for VS 2010 and older is

VS 2010 及更早版本的相应正则表达式为

^~(:Wh@//.+)~(:Wh@\{:Wh@)~(:Wh@\}:Wh@)~(:Wh@/#).+

回答by Christopher Klein

In Visual Studio Team System 2008 you can do from the menu Analyze--> 'Calculate Code Metrics for Solution' and it will give you a line count of your entire solution (among other things g)

在 Visual Studio Team System 2008 中,您可以从菜单 Analyze--> 'Calculate Code Metrics for Solution' 中进行操作,它将为您提供整个解决方案的行数(除其他外g