lines count

  • Thread starter Thread starter Bruce
  • Start date Start date
B

Bruce

Is there a mechanism within VS 2005 to determine the number of source code
lines in a C# project (either per file or across the entire project)?

Thanks, Bruce
 
I don't know of one in VS. Here is a quick an dirty console app that will
show you. Compile and put Linecount.exe in a dir in your path. Then just
run LineCount at the console in your project directory.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace LineCount
{
class Program
{
static int totalCount = 0;

static void Main(string[] args)
{
string path = Environment.CurrentDirectory;
DirectoryInfo di = new DirectoryInfo(path);
Console.WriteLine("Line count for all *.cs files in current
directory and all subdirectories.");
ShowProjectLineCount(di);
Console.WriteLine("Total Project Count: {0}", totalCount);
}

public static void ShowProjectLineCount(DirectoryInfo d)
{
// Curr dir.
FileInfo[] fis = d.GetFiles("*.cs");
foreach ( FileInfo fi in fis )
{
using ( StreamReader sr = fi.OpenText() )
{
string line;
int lineCount = 0;
while ( (line = sr.ReadLine()) != null )
{
lineCount++;
}
totalCount += lineCount;
Console.WriteLine("{0} {1}", lineCount, fi.FullName);
lineCount = 0;
}
}
// Subdirectories.
DirectoryInfo[] dis = d.GetDirectories();
foreach ( DirectoryInfo di in dis )
{
ShowProjectLineCount(di);
}
}
}
}
 
William
Works like a charm. You saved me easily 60+ minutes... if I had tried it
to code it myself.
Thanks,
Bruce

William Stacey said:
I don't know of one in VS. Here is a quick an dirty console app that will
show you. Compile and put Linecount.exe in a dir in your path. Then just
run LineCount at the console in your project directory.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace LineCount
{
class Program
{
static int totalCount = 0;

static void Main(string[] args)
{
string path = Environment.CurrentDirectory;
DirectoryInfo di = new DirectoryInfo(path);
Console.WriteLine("Line count for all *.cs files in current
directory and all subdirectories.");
ShowProjectLineCount(di);
Console.WriteLine("Total Project Count: {0}", totalCount);
}

public static void ShowProjectLineCount(DirectoryInfo d)
{
// Curr dir.
FileInfo[] fis = d.GetFiles("*.cs");
foreach ( FileInfo fi in fis )
{
using ( StreamReader sr = fi.OpenText() )
{
string line;
int lineCount = 0;
while ( (line = sr.ReadLine()) != null )
{
lineCount++;
}
totalCount += lineCount;
Console.WriteLine("{0} {1}", lineCount, fi.FullName);
lineCount = 0;
}
}
// Subdirectories.
DirectoryInfo[] dis = d.GetDirectories();
foreach ( DirectoryInfo di in dis )
{
ShowProjectLineCount(di);
}
}
}
}

--
William Stacey [MVP]

Bruce said:
Is there a mechanism within VS 2005 to determine the number of source
code lines in a C# project (either per file or across the entire
project)?

Thanks, Bruce
 
Back
Top