By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Counting code lines i asp.net project

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I’d suggest using DPack from USysWare (http://www.usysware.com/dpack/) for
this task. With it installed, it adds a few extra items to your Tools menu
including one named ‘Solution Statistics’ which runs through all of the files
and projects that are part of your solution and lists for you to total number
of lines within, as well as how many of those lines are empty, comments or
code.

Brendan
"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

Nov 17 '05 #2

P: n/a
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;
}
}
}

Nov 17 '05 #3

P: n/a
PErfect !
"Brendan Grant" <gr****@NOSPAMdahat.com> skrev i en meddelelse
news:44**********************************@microsof t.com...
I'd suggest using DPack from USysWare (http://www.usysware.com/dpack/) for
this task. With it installed, it adds a few extra items to your Tools menu
including one named 'Solution Statistics' which runs through all of the
files
and projects that are part of your solution and lists for you to total
number
of lines within, as well as how many of those lines are empty, comments or
code.

Brendan
"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

Nov 17 '05 #4

P: n/a
> including one named 'Solution Statistics' which runs through all of
the files
and projects that are part of your solution and lists for you to
total number
of lines within, as well as how many of those lines are empty,
comments or
code.


To add to Brendan's reply, you could also copy the stats dialog
contents to the clipboard via Ctrl-C. I find it useful if I need to
share the results with somebody.
--
Sergey Mishkovskiy
http://www.usysware.com/dpack/ - DPack - free VS.NET add-ons
http://www.usysware.com/blog/

Nov 17 '05 #5

P: n/a
There is a Statistics feature in my add-in (below). It counts code lines,
comment lines, total lines, percentages, classes, functions and so on...

--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Anders K. Jacobsen [DK]" <no**@at.all> escribi en el mensaje
news:uO*************@TK2MSFTNGP15.phx.gbl...
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

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.