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.Collections;
using System.ComponentModel;
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.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.btnRun = new System.Windows.Forms.Button();
this.txtDirectory = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnRun
//
this.btnRun.Location = new System.Drawing.Point(8, 40);
this.btnRun.Name = "btnRun";
this.btnRun.TabIndex = 0;
this.btnRun.Text = "Run";
this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
//
// txtDirectory
//
this.txtDirectory.Location = new System.Drawing.Point(8, 8);
this.txtDirectory.Name = "txtDirectory";
this.txtDirectory.Size = new System.Drawing.Size(256, 20);
this.txtDirectory.TabIndex = 1;
this.txtDirectory.Text = "C:\\Projects\\SOURCE_SAFE\\Code\\AIMHealth";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.txtDirectory);
this.Controls.Add(this.btnRun);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(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(object sender, System.EventArgs e)
{
_totalCount=0;
//Write out header
_sw.WriteLine("Directory|File|File Line Count|Directory Line Count");
//Get the starting directory
string startDir = txtDirectory.Text;
//startDir = @"C:\Projects\Temp\DataTest\DataTest";
//Set up the total count
_totalCount += GetLinesForDirectory(startDir);
//Write out footer
_sw.WriteLine("TOTAL||||"+_totalCount.ToString());
_sw.Flush();
_sw.Close();
MessageBox.Show(_totalCount.ToString());
}
private long GetLinesForDirectory(string directoryPath)
{
DirectoryInfo di = new DirectoryInfo(directoryPath);
long directoryCount = 0;
//Directories
foreach(DirectoryInfo d in di.GetDirectories())
{
_totalCount += GetLinesForDirectory(d.FullName);
}
//Files
foreach(FileInfo f in di.GetFiles())
{
if((f.Extension==".cs")||(f.Extension==".asax")||( f.Extension==".aspx")||(f.Extension==".asmx")||(f. Extension==".ascx"))
directoryCount += GetLinesForFile(f.FullName, directoryPath);
}
//Write out the line
if(directoryCount!=0)
{
_sw.WriteLine(directoryPath+"|||"+directoryCount.T oString());
}
return directoryCount;
}
private long GetLinesForFile(string filePath, string directoryPath)
{
long counter=0;
string line;
StreamReader sr = new StreamReader(filePath);
string[] fileBroken = null;
while(sr.Peek()>=0)
{
line=sr.ReadLine().Trim();
if(line.Length>0)
{
if((line.Substring(0,1)!=@"/")&&(line.Substring(0,1)!=@"*")&&(line.Substring(0 ,1)!=@"#"))
counter++;
}
}
if(counter!=0)
{
fileBroken = filePath.Split(_split);
_sw.WriteLine(directoryPath+"|"+fileBroken[fileBroken.Length-1]+"|"+counter.ToString());
}
return counter;
}
}
}