Line Count Utility

This article is updated to RTM by Patrick Wright.

Introduction

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.

Example

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()
        {
        }

        /// <summary>
        /// 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 reside
            String[] myDirectoryArray = new String[2] { "c:\\inetpub\\wwwroot\\supplynet\\", "d:\\Net Projects\\SpNetComponents\\" };

            // Loop through directories
            foreach (String sd in myDirectoryArray)
            {
                DirectoryInfo dir = new DirectoryInfo(sd);

                // Loop through file types
                foreach (String sFileType in myFileArray)
                {
                    // Loop through 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;
        }
    }
}


Recommended Free Ebook
Similar Articles