Line Count Utility


This article is updated to RTM by Patrick Wright.

Description

This utility is for counting number of code lines in a Visual Studio Project. it returns  the number of code lines and  file names which are in the project folders. Actually, you can use this utility for any kind of text file to count the number of lines. This is C# Class file, so you can easily call it in any Web or Windows application. I programmed a simple web application, which shows the number of code line  and file names in a web datagrid.

Do not forget to change:

String[] myDirectoryArray

this array contains the folder names in which my project files reside. Moreover, you can also increase the number of file types  if you change String[] myFileArray.

Source Code:

using System;
using System.Collections;
using System.IO;
namespace LineCount
{
/// <summary>
/// Application: It counts code lines in the VS.NET Project
/// Author: Levent Camlibel
/// Date: July 26, 2001
/// </summary>
public class DBLineCount
{
//FileNames holds the names of files in the project directories
protected ArrayList FileNames= new ArrayList(200);
public DBLineCount()
{
}
/// it returns filenames in the project
/// </summary>
public ArrayList FilesInProject
{
get
{
return FileNames;
}
}
/// <summary>
/// this function returns the count of code lines
/// </summary>
/// <returns></returns>
public int GetLineCount()
{
int LineCount=0;
// this array holds file types, you can add more file types if you want
String[] myFileArray = new String[7] { "*.cs","*.aspx", "*.ascx","*.xml","*.asax","*.config","*.js" };
// this array holds directories where your project files resides
String[] myDirectoryArray = new String[2]
{"c:\\inetpub\\wwwroot\\supplynet\\","d:\\Net Projects\\SpNetComponents\\" };
//this loops directories
foreach(String sd in myDirectoryArray)
{
DirectoryInfo dir =
new DirectoryInfo(sd);
// this loops file types
foreach(String sFileType in myFileArray)
{
// this loops files
foreach (FileInfo file in dir.GetFiles(sFileType))
{
// add the file name to FileNames ArrayList
FileNames.Add(file.FullName);
// open files for streamreader
StreamReader sr = File.OpenText(file.FullName);
//loop until the end
while (sr.ReadLine()!=null)
{
LineCount++;
}
//close the streamreader
sr.Close();
}
}
}
return LineCount;
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all