Counting code lines i asp.net project

  • Thread starter Anders K. Jacobsen [DK]
  • Start date
A

Anders K. Jacobsen [DK]

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
 
G

Guest

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
 
G

Guest

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 said:
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.ToString());
}

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;
}
}
}
 
A

Anders K. Jacobsen [DK]

PErfect !


Brendan Grant said:
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
 
S

Sergey M

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.
 
C

Carlos J. Quintero [.NET MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top