Accepted answer

no need to reinvent the wheel. take a look at the visual studio code metrics powertool 11.0


the code metrics powertool is a command line utility that calculates code metrics for your managed code and saves them to an xml file. this tool enables teams to collect and report code metrics as part of their build process. the code metrics calculated are:

• maintainability index

• cyclomatic complexity

• depth of inheritance

• class coupling

• lines of code (loc)

i know you said you don't have ultimate, so i just wanted to show you what you're missing.

code metrics in vs 2010 ultimate

for everyone else, there's sourcemonitor source monitor


ctrl+shift+f (find in files) -> put ";" in the "find what:"-textbox -> press "find all"-button.

this extremly simple method makes use of the fact, that any c# statement is terminated with a semicolon. and, at least i dont't use semicolons at any other place (e.g. in comments)...


i have no solid idea about them, but you can use code metrics values to get some statistics about your solution, like code lines.


we have used the tfs cube to get the data about how many lines add/delete/change on our tfs. this one you can view from excel. but need to configure it properly. and i don't think it will exclude the comments and blank lines etc.



private int countnumberoflinesincsfilesofdirectory(string dirpath)
    fileinfo[] csfiles = new directoryinfo(dirpath.trim())
                                .getfiles("*.cs", searchoption.alldirectories);

    int totalnumberoflines = 0;
    parallel.foreach(csfiles, fo =>
        interlocked.add(ref totalnumberoflines, countnumberofline(fo));
    return totalnumberoflines;

private int countnumberofline(object tc)
    fileinfo fo = (fileinfo)tc;
    int count = 0;
    int incomment = 0;
    using (streamreader sr = fo.opentext())
        string line;
        while ((line = sr.readline()) != null)
            if (isrealcode(line.trim(), ref incomment))
    return count;

private bool isrealcode(string trimmed, ref int incomment)
    if (trimmed.startswith("/*") && trimmed.endswith("*/"))
        return false;
    else if (trimmed.startswith("/*"))
        return false;
    else if (trimmed.endswith("*/"))
        return false;

           incomment == 0
        && !trimmed.startswith("//")
        && (trimmed.startswith("if")
            || trimmed.startswith("else if")
            || trimmed.startswith("using (")
            || trimmed.startswith("else  if")
            || trimmed.contains(";")
            || trimmed.startswith("public") //method signature
            || trimmed.startswith("private") //method signature
            || trimmed.startswith("protected") //method signature
  1. comments of // and /* kind are ignored.
  2. a statement written in multiple line is considered single line.
  3. brackets are (i.e. '{') not considered lines.
  4. 'using namespace' line are ignored.
  5. lines which are class name etc. are ignored.


visual studio will do this for you. right click on your project and choose calculate code metrics.

Related Query

More Query from same tag