Tuesday, September 21, 2010

Find number of lines Of Code (LOC) with LINQ

 

There are number of tools and projects available online to get the number of lines of code from your project. So what am i doing new? Well, actually there’s nothing new and I did not even cover all the scenarios. However, i’ve used the power of LINQ to achieve the results and doesn’t it look neat?

private static int CountNoOfLinesInApplication()
{
var CurrDirectory = @"C:\\ApplicationDirectory";
int lintCount = 0;
lintCount = (from t in Directory.GetFiles(CurrDirectory, "*.cs", SearchOption.AllDirectories)
where !t.EndsWith("AssemblyInfo.cs")
from line in File.ReadAllLines(t)
where line.Trim().Length > 1
&& (!line.StartsWith("//") || !line.StartsWith("///"))
select line).Count();
return lintCount;
}


I’ve written this program for my windows application and hence i wanted to avoid counting AssemblyInfo,cs file which is generated by visual studio, and i’ve also removed all blank lines and commented lines. This program only counts for .cs file, If you want to use this utility for ASP.net application and want to include .aspx files too then add an extra extension and you’re good to go :-)

No comments:

Post a Comment