Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » C# Language » Line Count Utility

Line Count Utility

Program returns count of code lines and file names in which code lines will be counted.

Page Views : 42854
Downloads : 379
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
LineCountLC.zip
 
 
Nevron Chart
Become a Sponsor
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


This article is updated to RTM by Patrick Wright.

Description

This utility is for counting number of code lines in a Visual Studio Project. it returns  the number of code lines and  file names which are in the project folders. Actually, you can use this utility for any kind of text file to count the number of lines. This is C# Class file, so you can easily call it in any Web or Windows application. I programmed a simple web application, which shows the number of code line  and file names in a web datagrid.

Do not forget to change:

String[] myDirectoryArray

this array contains the folder names in which my project files reside. Moreover, you can also increase the number of file types  if you change String[] myFileArray.

Source Code:

using System;
using System.Collections;
using System.IO;
namespace LineCount
{
/// <summary>
/// Application: It counts code lines in the VS.NET Project
/// Author: Levent Camlibel
/// Date: July 26, 2001
/// </summary>
public class DBLineCount
{
//FileNames holds the names of files in the project directories
protected ArrayList FileNames= new ArrayList(200);
public DBLineCount()
{
}
/// it returns filenames in the project
/// </summary>
public ArrayList FilesInProject
{
get
{
return FileNames;
}
}
/// <summary>
/// this function returns the count of code lines
/// </summary>
/// <returns></returns>
public int GetLineCount()
{
int LineCount=0;
// this array holds file types, you can add more file types if you want
String[] myFileArray = new String[7] { "*.cs","*.aspx", "*.ascx","*.xml","*.asax","*.config","*.js" };
// this array holds directories where your project files resides
String[] myDirectoryArray = new String[2]
{"c:\\inetpub\\wwwroot\\supplynet\\","d:\\Net Projects\\SpNetComponents\\" };
//this loops directories
foreach(String sd in myDirectoryArray)
{
DirectoryInfo dir =
new DirectoryInfo(sd);
// this loops file types
foreach(String sFileType in myFileArray)
{
// this loops files
foreach (FileInfo file in dir.GetFiles(sFileType))
{
// add the file name to FileNames ArrayList
FileNames.Add(file.FullName);
// open files for streamreader
StreamReader sr = File.OpenText(file.FullName);
//loop until the end
while (sr.ReadLine()!=null)
{
LineCount++;
}
//close the streamreader
sr.Close();
}
}
}
return LineCount;
}
}
}

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Levent Camlibel
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Dear Author - Thank you by Harshad On August 14, 2007
I had a very large file, was not getting opened thru any tool, but yes your tool has served my pupose. Appriciate your help to others. Harshad Hardas h.hardas@zensar.com
Reply | Email | Modify 
line of code by sneha On December 2, 2007
how to count no. of lines in vb6
Reply | Email | Modify 
Excellent! by Qi On August 27, 2008
Just what I needed to count my hundred or so lines of code! Very helpful...
Reply | Email | Modify 
Excellent! by Qi On August 27, 2008
Just what I needed to count my hundred or so lines of code! Very helpful...
Reply | Email | Modify 
Excellent! by Qi On August 27, 2008
Just what I needed to count my hundred or so lines of code! Very helpful...
Reply | Email | Modify 
nice idea by danny On April 29, 2009
nice idea put tool keeps crashing
Reply | Email | Modify 
Thanks a lot by Suresh On June 13, 2010
A very useful utility
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.