Line Count Utility in VB.NET


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 VB 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.

Dim myDirectoryArray as string [].

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 Dim myFileArray as    String[].

Source Code

Imports System
Imports System.Collections
Imports 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 FileNames As New ArrayList(200)
Public Sub New()
End Sub 'New
'/ it returns filenames in the project
'/ </summary>
Public ReadOnly Property FilesInProject() As ArrayList
End Get
'/ <summary>
'/ this function returns the count of code lines
'/ </summary>
'/ <returns></returns>
Public Function GetLineCount() As Integer
 LineCount As Integer = 0
' this array holds file types, you can add more file types if you want
Dim myFileArray(7) As [String] = {"*.cs", "*.aspx", "*.ascx", "*.xml", "*.asax", "*.config", "*.js"}
' this array holds directories where your project files resides
Dim myDirectoryArray(2) As [String] = {"c:\inetpub\wwwroot\supplynet\", "d:\Net Projects\SpNetComponents\"}
'this loops directories
Dim sd As [String]
For Each sd In myDirectoryArray
Dim dir As New DirectoryInfo(sd)
' this loops file types
Dim sFileType As [String]
For Each sFileType In myFileArray
' this loops files
Dim file As FileInfo
For Each file In dir.GetFiles(sFileType)
' add the file name to FileNames ArrayList
' open files for streamreader
Dim sr As StreamReader = File.OpenText(file.FullName)
'loop until the end
While Not (sr.ReadLine() Is Nothing)
LineCount += 1
End While
'close the streamreader
Next file
Next sFileType
Next sd
Return LineCount
End Function 'GetLineCount
End Class 'DBLineCount
End Namespace 'LineCount

Similar Articles