Hi ,

I wanted to know how to write a c# code that reads from a file and counts the number of lines . This count should exclude the comments present

eg :

//this is test
using System;

should not count "//this is test" at all .

Please can someone help me on this one

Thanks
shuchi

you could use the line.BeginsWith or line.StartsWith (forgot which one it is), and pass in "//". If it returns true, then don't count the line and go to the next line.

i gotta use regex.match(regularexpression) . I am using a streamreader to read the whole file and then the contents of the file are match with the pattern of the regular expression . and this regular expression needs to count all the lines of code excluding watever is written in comments .

eg

//test1
//test2
/* test3 */
using System;
using System.Collections.Generic; /* test4 */
using System.Text;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace Regular_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Good");
}
}
}

this shud remove all the comments .. ie shud count all the line of code except comments (/*..*/) , (//) wherever they appear .

Well the idea would be to write a very trivial regex expression and then apply it on a code snippet. Then you would add to that regex expression gradually getting more complex.

well well well ,
i made a C# program that now reads a file and excluding the comments out.. that worked .. thanks to all . :)

This article has been dead for over six months. Start a new discussion instead.