Click here to Skip to main content
Click here to Skip to main content

Counting Lines of Code in C#

, 19 Dec 2004
Rate this:
Please Sign up or sign in to vote.
An article on recursively counting lines of code in files from a given directory.

Introduction

I present to you a tool that is capable of... Counting Lines!

Wow! you say in amazement as you stagger back trying to regain your balance. That's right my friends, I am afraid there is no ground breaking stuff here today.

However having said that, I had to create this tool because I could not easily find anything else out there that would do what I was after. Thus if you bear with me, you might find this code useful to you.

There are two parts to this article you may find interesting. The first is the DirectoryLineCounter. This is the heart of the article and is a simple class that will recursively extract the number of lines from a subset of files from a given directory.

The second interesting part of the download is the application that uses the DirectoryLineCounter. This application allowed us to rapidly work out exactly just how much code was contained in the various sections of our repository.

This information was useful to us in identifying where people were creating the most code in our scientific framework. We were hoping to see that the most code effort was being put into the creation of science, but instead we saw that the applications (GUI's) that were utilizing the science framework were where the most lines of code were being recorded.

Line count engine

We were initially using a simple linecounter (grep/script) to give us the total number of lines in our entire repository, but this didn't really give us any useful information as to what areas of the repository contained the most code. Thus we created this simple class that was capable of recursing into the directories and reporting back the information in a structured way.

The DirectoryLineCounter class has two static arrays, DirectoryIgnoreNames and FileSearchPatterns. These are the directories to ignore (i.e. bin, debug, .cvs, .svn ...), and the file types to count (i.e.. *.cs, *.h, *.vb ...). Having them as static fields was fine for our use because DirectoryLineCounter was only ever run with the intention of summarizing one directory (and its subdirectories) in the one run. It would be a simple change to make the member fields, and pass them as parameters to the recursive runs.

Once the DirectoryIgnoreNames and FileSearchPatterns fields have been set, the DirectoryLineCounter is able to produce some useful results by calling the countLines() method. Once complete the DirectoryLineCounter will contain two counts, one for the lines of code found in the directory it was pointing at (DirectoryLines), and another count for the total lines found in all subdirectories (SubDirectoriesTotalLines). The DirectoryLineCounter also contains a list of DirectoryLineCounter's that represent all the subdirectories in the initial directory, this array is the SubDirectoryCounters field.

The last thing that might need explaining is the FilesCompleted event. This event is fired whenever a DirectoryLineCounter has finished counting from all the files in its directory. It then passes back in the event the number of files just completed. This was useful for giving the user progress of where the process was at.

The DirectoryLineCounter was put into a separate project (LineCountEngine) so that it was easy to create many applications from the same project. I intended to write a command line application that would also utilize the LineCountEngine, but this is not likely to happen given the current time constraints.

As for performance, I have no idea what is good, but I can tell you it takes about 10 seconds to summarize our repository of around 650,000 lines. This is not a problem for us.

Line counter application

The line counter application simply made use of the LineCountEngine. You tell it where to start and press go, the application then builds a directory tree with the information the DirectoryLineCounter returns.

Thanks to Julijan Sribar for the use of his PieChart component. You can see his article here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Seato
Web Developer
Australia Australia
I have been a software engineer since graduating in 1999, and am working for a govenment organisation in Australia developing a software framework for scientists. I have been programming in C#/.Net since 2002 and I really think it is the bees-knees of computer languages/platforms. I still dable a little in C++, but that is mainly game programming for fun.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberfmmanrique5-Mar-12 5:50 
GeneralMy vote of 4 PinmembercFullerene24-Oct-11 6:31 
GeneralMy vote of 4 Pinmemberseapanther5-Mar-11 10:10 
Questionwhat i was looking for Pinmemberxavex1-Apr-10 10:48 
although 3/4 of code base is devoted to drawing the pie chart Smile | :)
 
thanks,
Piotr
GeneralA deceptive title. Pinmembermathias.deguignet31-Aug-09 5:01 
GeneralExcellent tool PinmemberAvi Farah13-Aug-09 8:16 
GeneralIt is useful tool Pinmemberccache4-Jun-09 8:43 
GeneralWonderful, and also works under Mono PinmemberWolfgang Schulze-Zachau5-Feb-09 13:00 
GeneralAmazing tool Pinmembersumitkm29-Jan-09 10:30 
GeneralJust what I needed PinmemberAbhishek Tiwari17-Dec-08 18:09 
Questionneed more Pinmemberniranjan singh7-Nov-05 21:46 
AnswerRe: need more PinmemberSeato9-Nov-05 17:16 
GeneralI have a problem compiling this without Visual Studio Pinmemberselp6-Aug-05 7:15 
GeneralRe: I have a problem compiling this without Visual Studio (solution) Pinmemberselp6-Aug-05 21:51 
Generalanother source code file analyzer PinmemberKingMarine27-Jan-05 15:59 
GeneralCounting everything, not only LOC PinmemberThalendar18-Jan-05 6:44 
GeneralRe: Counting everything, not only LOC PinmemberSeato10-Aug-05 14:09 
GeneralRe: Counting everything, not only LOC Pinmemberfpmorrison21-Dec-05 8:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 19 Dec 2004
Article Copyright 2004 by Seato
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid