How do you count the lines of code in a Visual Stu

2019-01-03 11:28发布

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?

25条回答
神经病院院长
2楼-- · 2019-01-03 11:50

Other simple tool For VS2008 (open source): http://www.accendo.sk/Download/SourceStat.zip

查看更多
forever°为你锁心
3楼-- · 2019-01-03 11:51

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.

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 Results window, you will see "Matching Lines" which is the lines of code count.


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

This RegEx excludes the following items:


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

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

HTML Comments:

<!-- 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:

        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.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-03 11:52

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)

查看更多
爷、活的狠高调
5楼-- · 2019-01-03 11:52

Obviously tools are easier, but I feel cool doing this in powershell:)

This script finds all the .csproj references in the .sln file, and then within each csproj file it locates files included for compilation. For each file that is included for compilation it creates an object with properties: Solution, Project, File, Lines. It stores all these objects in a list, and then groups and projects the data as needed.

#path to the solution file e.g. "D:\Code\Test.sln"
$slnFile = "D:\Code\Test.sln"


#results
$results = @()

#iterate through .csproj references in solution file
foreach($projLines in get-item $slnFile | Get-Content | Select-String '".*csproj')
{
    $projFile = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($slnFile), [regex]::Match($projLines,'[^"]*csproj').Value)
    $projFolder = [System.IO.Path]::GetDirectoryName($projFile)

    #from csproj file: get lines for files to compile <Compile Include="..."/>
    $includeLines = get-item $projFile | Get-Content | Select-String '<Compile Include'


    #count of all files lines in project
    $linesInProject = 0;
    foreach($fileLine in $includeLines)
    {
        $includedFilePath = [System.IO.Path]::Combine($projFolder, [Regex]::Match($fileLine, '"(?<file>.*)"').Groups["file"].Value)
        $lineCountInFile = (Get-Content $includedFilePath).Count      
        $results+=New-Object PSObject -Property @{ Solution=$slnFile ;Project=$projFile; File=$includedFilePath; Lines=$lineCountInFile }
    }
}

#filter out any files we dont need
$results = $results | ?{!($_.File -match "Designer")}


#print out:

"---------------lines per solution--------------"
$results | group Solution | %{$_.Name + ": " + ($_.Group | Measure-Object Lines -Sum).Sum}
"---------------lines per peoject--------------"
$results | group Project | %{$_.Name + ": " + ($_.Group | Measure-Object Lines -Sum).Sum}
查看更多
姐就是有狂的资本
6楼-- · 2019-01-03 11:58

In Visual Studio 2015 go to the Analyze Menu and select "Calculate Code Metrics".

查看更多
女痞
7楼-- · 2019-01-03 11:59

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

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:

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? :)

查看更多
登录 后发表回答