Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Counting Lines of Code in C#

Rate me:
Please Sign up or sign in to vote.
3.56/5 (29 votes)
19 Dec 20043 min read 125.9K   5.7K   32   19
An article on recursively counting lines of code in files from a given directory.

Image 1

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


Written By
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

 
Question.NET Standard Library Version Pin
ReadWatchCreate23-Feb-18 14:29
ReadWatchCreate23-Feb-18 14:29 
GeneralMy vote of 5 Pin
fmmanrique5-Mar-12 5:50
fmmanrique5-Mar-12 5:50 
GeneralMy vote of 4 Pin
cFullerene24-Oct-11 6:31
cFullerene24-Oct-11 6:31 
GeneralMy vote of 4 Pin
MrSeapanther5-Mar-11 10:10
MrSeapanther5-Mar-11 10:10 
Questionwhat i was looking for Pin
xavex1-Apr-10 10:48
xavex1-Apr-10 10:48 
GeneralA deceptive title. Pin
mathias.deguignet31-Aug-09 5:01
mathias.deguignet31-Aug-09 5:01 
GeneralExcellent tool Pin
Avi Farah13-Aug-09 8:16
Avi Farah13-Aug-09 8:16 
GeneralIt is useful tool Pin
ccache4-Jun-09 8:43
ccache4-Jun-09 8:43 
GeneralWonderful, and also works under Mono Pin
Wolfgang Schulze-Zachau5-Feb-09 13:00
Wolfgang Schulze-Zachau5-Feb-09 13:00 
GeneralAmazing tool Pin
sumitkm29-Jan-09 10:30
sumitkm29-Jan-09 10:30 
GeneralJust what I needed Pin
Abhishek Tiwari17-Dec-08 18:09
Abhishek Tiwari17-Dec-08 18:09 
Thanks for the cool tool dude. It was exactly what I was looking for!

Good work!!!
Questionneed more Pin
Niranjan Singh7-Nov-05 21:46
Niranjan Singh7-Nov-05 21:46 
AnswerRe: need more Pin
Seato9-Nov-05 17:16
Seato9-Nov-05 17:16 
GeneralI have a problem compiling this without Visual Studio Pin
Member 21750386-Aug-05 7:15
Member 21750386-Aug-05 7:15 
GeneralRe: I have a problem compiling this without Visual Studio (solution) Pin
Member 21750386-Aug-05 21:51
Member 21750386-Aug-05 21:51 
Generalanother source code file analyzer Pin
KingMarine27-Jan-05 15:59
KingMarine27-Jan-05 15:59 
GeneralCounting everything, not only LOC Pin
User 70694918-Jan-05 6:44
User 70694918-Jan-05 6:44 
GeneralRe: Counting everything, not only LOC Pin
Seato10-Aug-05 14:09
Seato10-Aug-05 14:09 
GeneralRe: Counting everything, not only LOC Pin
fpmorrison21-Dec-05 8:36
fpmorrison21-Dec-05 8: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.