C#, Visual Basic and C++ .NET Line Counter Utility – Version 2


Source code download.

Executable download.


An updated version of the line count utility described in an earlier article is now available. This now:

  • Does basic C++ line counting of Visual Studio.NET projects and solutions, as well as C# and VB.NET as previously.
  • Counts the total number of code files and displays them in the summary section.
  • Allows the summary to be selected (by dragging over it) and copied to the clipboard with Ctrl-C.

Unfortunately for C++ projects I don’t think there’s an easy way of identifying auto-generated code, and hence the application will always return zero for code-generated lines for C++ projects.

Code Project

An abbreviated version of these articles is available on the Code Project.

30 thoughts on “C#, Visual Basic and C++ .NET Line Counter Utility – Version 2

  1. Hi Rich,

    I found this via the Code Project.

    I’m trying to generate useful information about Lines of Code during the build process. I can’t tell from the articles if there is a command line option or a non-interactive (html/xml) output option. Have you considered using such?

  2. Hi!
    Thank you for the great utility. When I am analysing a vb.net 2003 project, I get the following error message in FindCodeFilesAndAddToChildContainersList: Invalid character in xml.
    The reason is, that a filename in my *.proj contains a german umlaut (Ü), which is not XML-conform. Do you know a quick workaround?


  3. Hi Rich,

    Thanks for a great tool. I have one question, I tried it on a web service project (2005) and it did not pick up the program files in the app_code folder. Is there a setting that I can make to have it do that?


  4. Hi,

    I found your application extremely useful. I made a couple of modifications to it if you are interested.

    We use a lot of codeSmith templates in our projects so I wanted to to take the templates, and generated code into consideration. I also wanted to process .dbp prjoject (database projects).

    I modified the code to handle .dbp, .sql, and .cst (code smith template) files and it seems to work fine. I had to modify the dataset, add some counters, etc. I also updated it to 3.5 of the framework and use some Linq stuff for array processing and the like.

    If you are interested in the changes let me know and I can zip and get them to you.

    Thanks again for this head start, your code was easy to read and modify and was a pleasure to work with.


  5. I was looking for a way to count lines in a large web application that I developed at work, and found this. It didn’t quite work for me out of the box (.NET Web Projects don’t have project files so this utility only found and counted my Helpers code, which is compiled in it’s own proj file to a DLL). However with the great documentation and design of the application I was able to modify the project in about an hour to do exactly what I wanted.

    Thanks for the great utility, and for making it open-source so I could make it work for me too!

    PS: If you want the code to make it work with Web Projects shoot me an email, I’d be happy to send it over. I did it with very minor changes to the CodeContainer and Factory classes and adding some items to the interface, all of which were needed to allow the utility to count based on Directory and added a class (called WebProject) which inherits and implements CodeContainer.

  6. thnx for this great tool… if you just want to use it for counting lines etc. it is even better then code metric in vs.

  7. Thanks! This is quite useful.

    One great feature would be the ability to export the data generated to various formats – e.g., .xls, .html, etc.

  8. I use LineCount to count lines in C++ files based on .NET Solution. I found out that all files with relative path (i.e. ..\..\filename) has 0 lines
    Why ?

    1. I had this same problem so I downloaded the source code, and took a look.
      Anyway, I fixed the ..\..\ by doing the following (no guarantees about fixing or breaking other relative paths though):

      Open the Project.cs file in the CodeContainers folder.
      Look for the AddCodeFileToList function (line 69 in mine).

      Locate the following line of code in the function (line 73 in mine):
      if (codeFileRelativeName.StartsWith(“.”)) codeFileRelativeName = codeFileRelativeName.Substring(2);

      Comment this line out, ie:
      //if (codeFileRelativeName.StartsWith(“.”)) codeFileRelativeName = codeFileRelativeName.Substring(2);

      Or replace it with:
      if (codeFileRelativeName.StartsWith(“.\\”)) codeFileRelativeName = codeFileRelativeName.Substring(2);

      After I did this, all paths with ..\..\ loaded correctly.
      The reason this was breaking was because the path was being changed from something like “..\\..\\SomeFileName.cs” to “\\..\\SomeFileName.cs”

      Anyway, it ‘should’ be fairly safe to comment the line out as the ‘Path.Combine’ function seems to be intelligent enough to handle relative paths (at least on mine it does).

      The 2nd alternative basically checks to see if the path starts with .\ and omits it, but leaves paths with ..\ alone.

  9. Thank you very much for this, Rich.

    I have made the necessary changes so it also works with Visual Studio 2010 C++ projects. Email me at “email at rennie-petersen dot eu” if you’d like me to send these changes to you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s