Click here to Skip to main content
13,351,050 members (53,976 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 19 Dec 2004

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.


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.


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


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
fmmanrique5-Mar-12 6:50
memberfmmanrique5-Mar-12 6:50 
GeneralMy vote of 4 Pin
cFullerene24-Oct-11 7:31
membercFullerene24-Oct-11 7:31 
GeneralMy vote of 4 Pin
seapanther5-Mar-11 11:10
memberseapanther5-Mar-11 11:10 
Questionwhat i was looking for Pin
xavex1-Apr-10 11:48
memberxavex1-Apr-10 11:48 
GeneralA deceptive title. Pin
mathias.deguignet31-Aug-09 6:01
membermathias.deguignet31-Aug-09 6:01 
GeneralExcellent tool Pin
Avi Farah13-Aug-09 9:16
memberAvi Farah13-Aug-09 9:16 
GeneralIt is useful tool Pin
ccache4-Jun-09 9:43
memberccache4-Jun-09 9:43 
It's a good tool for me. Thanks Seato.
GeneralWonderful, and also works under Mono Pin
Wolfgang Schulze-Zachau5-Feb-09 14:00
memberWolfgang Schulze-Zachau5-Feb-09 14:00 
GeneralAmazing tool Pin
sumitkm29-Jan-09 11:30
membersumitkm29-Jan-09 11:30 
GeneralJust what I needed Pin
Abhishek Tiwari17-Dec-08 19:09
memberAbhishek Tiwari17-Dec-08 19:09 
Questionneed more Pin
niranjan singh7-Nov-05 22:46
memberniranjan singh7-Nov-05 22:46 
AnswerRe: need more Pin
Seato9-Nov-05 18:16
memberSeato9-Nov-05 18:16 
GeneralI have a problem compiling this without Visual Studio Pin
selp6-Aug-05 8:15
memberselp6-Aug-05 8:15 
GeneralRe: I have a problem compiling this without Visual Studio (solution) Pin
selp6-Aug-05 22:51
memberselp6-Aug-05 22:51 
Generalanother source code file analyzer Pin
KingMarine27-Jan-05 16:59
memberKingMarine27-Jan-05 16:59 
GeneralCounting everything, not only LOC Pin
Thalendar18-Jan-05 7:44
memberThalendar18-Jan-05 7:44 
GeneralRe: Counting everything, not only LOC Pin
Seato10-Aug-05 15:09
memberSeato10-Aug-05 15:09 
GeneralRe: Counting everything, not only LOC Pin
fpmorrison21-Dec-05 9:36
memberfpmorrison21-Dec-05 9:36 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 19 Dec 2004
Article Copyright 2004 by Seato
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid