At the end of this post is a C# program I had to count all lines of code in a
single directory tree. Not exactly what you had in mind, but you could parse
through the solution files to find the particular projects you are interested
in.
I have thought about going back through the code and making a Visual Studio
add in, but I am wracked at this time. I am sure there are code metric tools
that are far better than this.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
*************** ************
Think Outside the Box!
*************** ************
"Anders K. Jacobsen [DK]" wrote:
Hi
We have a rather large asp.net project with serveral utility projects
(written in C#). Is there at tool out there which can give an estimate of
the total amount of code lines all projects results in?
thanks in regads
Anders
/* CODE SAMPLE *************** *************** ************/
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.IO;
namespace LineCounter
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Button btnRun;
private System.Windows. Forms.TextBox txtDirectory;
private System.Componen tModel.Containe r components = null;
public Form1()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.btnRun = new System.Windows. Forms.Button();
this.txtDirecto ry = new System.Windows. Forms.TextBox() ;
this.SuspendLay out();
//
// btnRun
//
this.btnRun.Loc ation = new System.Drawing. Point(8, 40);
this.btnRun.Nam e = "btnRun";
this.btnRun.Tab Index = 0;
this.btnRun.Tex t = "Run";
this.btnRun.Cli ck += new System.EventHan dler(this.btnRu n_Click);
//
// txtDirectory
//
this.txtDirecto ry.Location = new System.Drawing. Point(8, 8);
this.txtDirecto ry.Name = "txtDirecto ry";
this.txtDirecto ry.Size = new System.Drawing. Size(256, 20);
this.txtDirecto ry.TabIndex = 1;
this.txtDirecto ry.Text = "C:\\Projects\\ SOURCE_SAFE\\Co de\\AIMHealth";
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.txtDire ctory);
this.Controls.A dd(this.btnRun) ;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private long _totalCount=0;
StreamWriter _sw = new StreamWriter(@" C:\temp\counts. txt");
private char[] _split = { '\\' };
private void btnRun_Click(ob ject sender, System.EventArg s e)
{
_totalCount=0;
//Write out header
_sw.WriteLine(" Directory|File| File Line Count|Directory Line Count");
//Get the starting directory
string startDir = txtDirectory.Te xt;
//startDir = @"C:\Projects\T emp\DataTest\Da taTest";
//Set up the total count
_totalCount += GetLinesForDire ctory(startDir) ;
//Write out footer
_sw.WriteLine(" TOTAL||||"+_tot alCount.ToStrin g());
_sw.Flush();
_sw.Close();
MessageBox.Show (_totalCount.To String());
}
private long GetLinesForDire ctory(string directoryPath)
{
DirectoryInfo di = new DirectoryInfo(d irectoryPath);
long directoryCount = 0;
//Directories
foreach(Directo ryInfo d in di.GetDirectori es())
{
_totalCount += GetLinesForDire ctory(d.FullNam e);
}
//Files
foreach(FileInf o f in di.GetFiles())
{
if((f.Extension ==".cs")||(f.Ex tension==".asax ")||(f.Extensio n==".aspx")||(f .Extension==".a smx")||(f.Exten sion==".ascx"))
directoryCount += GetLinesForFile (f.FullName, directoryPath);
}
//Write out the line
if(directoryCou nt!=0)
{
_sw.WriteLine(d irectoryPath+"| ||"+directoryCo unt.ToString()) ;
}
return directoryCount;
}
private long GetLinesForFile (string filePath, string directoryPath)
{
long counter=0;
string line;
StreamReader sr = new StreamReader(fi lePath);
string[] fileBroken = null;
while(sr.Peek() >=0)
{
line=sr.ReadLin e().Trim();
if(line.Length> 0)
{
if((line.Substr ing(0,1)!=@"/")&&(line.Subst ring(0,1)!=@"*" )&&(line.Substr ing(0,1)!=@"#") )
counter++;
}
}
if(counter!=0)
{
fileBroken = filePath.Split( _split);
_sw.WriteLine(d irectoryPath+"| "+fileBroke n[fileBroken.Leng th-1]+"|"+counter.To String());
}
return counter;
}
}
}