Click here to Skip to main content
11,930,069 members (44,345 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked

Extensible Line Code Counter

, 2 Jan 2009 LGPL3
Rate this:
Please Sign up or sign in to vote.
Utility for counting lines of source code. It can easily be extended to count any source code types


Despite google, despite all of the other code sites, I cannot seem to find a program that reliable counts the number of code lines in my projects. Given my interesting in making small flexible tools, I decided it was time to build my own program for counting lines of code.


Source Code
Documentation (word .doc)
VB Extension Example


From the start, I wanted to build a code line counter that was flexible. So I separated the logic that tabulates the counts from the logic that determines what is “countable”. I also made the logic that determines what is countable, loaded at runtime. These two decisions make for a dynamic and flexible utility program. CodeCounter is easily extended to process other source code types, such as fortran. It is as simple as implementing a few functions defined in an interface.

What does CodeCounter do

It counts the lines of code in source files. Current source code type supported are C#, Xaml and SQL. CodeCounter can easily be extended to count source code from other file types. Read on to learn how.


If you downloaded the binaries, you need to copy all of the files in the ZIP into some location. I suggest some where in your path so that the program is always accessible. In the binary zip file you should have found the following files:

Big Woo.NET.dll  




All of the files need to be copied into the same location. For me, I copied my files to C:\Program Files\tools. You should pick what works best for you.

Using CodeCounter

From the command line, just run the program with no inputs:


CodeCounter by matt raffel

(c) 2009 -







FileName can be full filename or partial with wild card

As you can see, there's not much. Pretty simple. Here's what the commands mean and do:


by default the only information shown on the screen is the results of the line counting. Errors are also displayed, of course. When including this option, detail information is listed, listing each file found.


by default CodeCounter ingores subdirectories. Recurse turns off this behavior indicating searches to include subdirectories looking for matching input.


shows the above itty bitty help


this is the important input. {filename} is the file or wildcard file mask of the files you want counted. You can have more than one {filename} input. {filename} can be relative or literal path to a file and include any of the standard windows wildcards.


Opens the webpage


Lists the file types that CodeCounter is configured to process, and it shows the class responsible for the file type processing

The command line arguments can be entered in any order. The only command argument that is required is the filenames.



C:\src>CodeCounter *.cs

This example expects all C# files in the current directory to be counted. C# files in subdirectories will not be counted.

C:\src>CodeCounter test.cs

This example says to count the code for test.cs in the current directory and that's it.

C:\src>CodeCounter test.cs *.sql -recurse

This command expects test.cs and all .sql files, including those in subdirectories to be counted. There is a little bit of a surprise here, in that all test.cs files will be counted.

C:\src>CodeCounter test.cs *.sql -verbose

Same as the previous command, except the amount of output display is significantly more.

Extending CodeCounter

One of the key design features of my utility is the ease of extending CodeCounter to include additional file types.

If you are familiar with .NET, and particular C#, then you will find extending codecounter is very simple.

Before you get started, you might want to get the latest code first. You do not need the code, but it might be more instructive to look at the C# code counting implementation (class CSharpCodeCounterLogic) for understanding how to implement your own code counter.

In summary:

  1. To start, create your own .NET project.

  2. Add a reference to the CodeCounter.Library.dll.

  3. Create a new class that implements the ICodeCounterLogic interface.

  4. Write your code, build the assembly.

  5. Copy the assembly, preferrable to the same location as CodeCounter.exe.

  6. Edit CodeCounter.exe.config, updating the codeCounters section with your new information.

Caution: I would caution against adding your types to CodeCounter.Library.dll as any updates from me would cause you to lose your changes. I encourage you to put your ICodeCounterLogic implementation in its own assembly.

The ICodeCounterLogic interface

CodeCounterLineType LineType(string line)

This is the logic method of the interface. The engine calls this method for each line read in from the a file.

Return: CodeCounterLineType

string[] FileTypesHandled()

Method defines the types supported by the interface implementation. At a minimum, a one element array should be returned. This method is used by the help “engine”.


Example: new string[] {"cs"};

bool CanProcessFile(string file)

Method determines if the interface implementation is able to process the input file. This method should examine the file to determine if the file contains code it understands. In most cases, evaluating file name extension might be sufficent.

Return: false or true

bool EngineCanDetermineBlankLines()

Currently unused method.

void PrefileProcessing(string fileName)

Called by the engine prior to opening the file. This gives the interface implementation an opportunity to perform any additional work it might need prior to beginning line counting.

Caution: if you open the file, make sure you close it or you open it in shared mode, or the engine will not be able to open the file.

void PostfileProcessing(string fileName)

Called by the engine after the file is closed, and successfully counting the lines of the file.

The CodeCounterLineType enum


Indicates an empty Line


Indicates the line contains code only


Indicates the line contains both code and comments


Indicates the line is a comment


Indicates a statement.  


Indicates a statement followed by a comment

The codecounter.exe.config file

Once you have your assembly built (and assuming unit tested), you will want to configure CodeCounter to include your assembly. This is done by editing the CodeCounter config file, adding your assembly to its collection of assembles.


<logic key="CS"


"CSharpCodeCounterLogic, CodeCounter.Library.CSharpCodeCounterLogic, CodeCounter.Library.dll"/>


Look for the counterImplementations section in the config file. You will want to duplicate one of the existing lines in this section. Copy or create a new “logic” line. For key, you can put anything you like as long as it is unique. Since file extension is unique, I have been using that.

For assemblyInformation, you enter the information about your assembly. The format of this field is familiar, however I have to warn you it does not conform to the standards. You need to enter, in comma delimited fashion, the class name, the class name with complete namespace, and the assembly name.

In the example above, the class name is CSharpCodeCounterLogic. The class name with namespace is CodeCounter.Library.CSharpCodeCounterLogic. And the assemby name is CodeCounter.Library.dll.

For the assembly name, if you exclude a path, it will assume the assembly is in the same folder as CodeCounter.exe.


VB Extension Example 

If you download the VB extension example, you will find an updated CodeCounter able to count lines of code in VB.NET files (*.vb).    Source code included!  Smile | :)  

I feel the VB example shows how simple it is to add new source code types to CodeCounter.

I hope you find my utility program useful.   I look forward to hearing from you.



2009.01.03 - I updated to the code (code only), to separate the reporting of lines and statements.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionBroken download links Pin
blueacorn20-May-14 13:46
memberblueacorn20-May-14 13:46 
AnswerRe: Broken download links Pin
mattraffel3-Sep-14 8:19
membermattraffel3-Sep-14 8:19 
GeneralMy vote of 4 Pin
blueacorn10-Oct-13 14:55
memberblueacorn10-Oct-13 14:55 
GeneralPointless Pin
PIEBALDconsult2-Jan-09 13:27
memberPIEBALDconsult2-Jan-09 13:27 
GeneralRe: Pointless Pin
lateral2-Jan-09 13:54
memberlateral2-Jan-09 13:54 
GeneralRe: Pointless Pin
PIEBALDconsult2-Jan-09 15:34
memberPIEBALDconsult2-Jan-09 15:34 
It's a long way down.
GeneralRe: Pointless Pin
AndrewSmith2-Jan-09 15:57
memberAndrewSmith2-Jan-09 15:57 
GeneralRe: Pointless Pin
PIEBALDconsult2-Jan-09 16:30
memberPIEBALDconsult2-Jan-09 16:30 
GeneralRe: Pointless [modified] Pin
mattraffel3-Jan-09 5:40
membermattraffel3-Jan-09 5:40 
GeneralRe: Pointless Pin
PIEBALDconsult3-Jan-09 6:32
memberPIEBALDconsult3-Jan-09 6:32 
GeneralRe: Pointless Pin
mattraffel5-Jan-09 13:45
membermattraffel5-Jan-09 13:45 
QuestionHow about ... Pin
sam.hill2-Jan-09 12:29
membersam.hill2-Jan-09 12:29 
AnswerRe: How about ... Pin
mattraffel3-Jan-09 5:49
membermattraffel3-Jan-09 5:49 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 2 Jan 2009
Article Copyright 2009 by mattraffel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid