How to find Lines of Code in VS 2010?
Tools > option >
select Text editor, line checkbox true
Thanks for your reply. The 'Line Number' checkbox only shows the line number for each line which starts from one in each form which I am already using. I want to find out how many lines are there in the total project (with many forms) at one go without adding bookmarks.
See if this helps.
Public Class Form1 '// your application's folder that contains all Forms. Private myAppFolder As String = "C:\Users\codeorder\Desktop\vb.net\WindowsApplication1\WindowsApplication1" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myCoolFileLineCount As Integer = 0 For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _ (myAppFolder, FileIO.SearchOption.SearchTopLevelOnly, "*.*") '// scan folder. If IO.Path.GetExtension(myCoolFile) = ".vb" Then '// get all files with the .vb file extension. If Not IO.Path.GetFileName(myCoolFile).Contains(".Designer.vb") Then '// remove files that are Designer files. Dim myCoolForm As String = IO.Path.GetFullPath(myCoolFile) '// get full path of file. Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolForm) '// load your file as a string array. myCoolFileLineCount += myCoolFileLines.Length '// count lines and add to the sum total. End If End If Next '// display your application's total code line count. MsgBox(Application.ProductName & vbNewLine & "Total Code Lines Count: " & myCoolFileLineCount, MsgBoxStyle.Information) End Sub End Class
Please supply the proper information when FIRST asking a question.
Edited by codeorder: n/a
Thanks for this code. It worked perfectly.
Hi, I was searching for an example about how to count lines, this was great but it's not completely useful when your application has nested folders with projects inside.
I had to rewrite the code to be recursive and count all lines of code of my solution, the final solution is the next:
Public Class frmCheckLineCount Private Function LinesInFile(ByVal myCoolFile As String) As Double Dim ret As Double = 0 lblFile.Text = IO.Path.GetFileName(myCoolFile) System.Windows.Forms.Application.DoEvents() If IO.Path.GetExtension(myCoolFile) = ".vb" Then '// get all files with the .vb file extension. If Not IO.Path.GetFileName(myCoolFile).Contains(".Designer.vb") Then '// remove files that are Designer files. Dim myCoolForm As String = IO.Path.GetFullPath(myCoolFile) '// get full path of file. ret += IO.File.ReadAllLines(myCoolForm).Length '// load your file as a string array. End If End If Return ret End Function Private Function LinesInFolder(ByVal folder As String) As Double Dim ret As Double = 0 For Each fileI As String In IO.Directory.GetFiles(folder) lblFolder.Text = folder System.Windows.Forms.Application.DoEvents() ret += LinesInFile(fileI) System.Threading.Thread.Sleep(20) Next For Each folderF As String In IO.Directory.GetDirectories(folder) lblFolder.Text = folderF System.Windows.Forms.Application.DoEvents() ret += LinesInFolder(folderF) Next Return ret End Function Private Sub btnCheckLineCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckLineCount.Click txtLineCount.Text = "LineCount: " & LinesInFolder(txtSolutionPath.Text) End Sub End Class
There are several calls to make pauses and make it more visually interesting and to show the directory/file being checked.
This implementation needs several controls to work correctly, the code explains itself how to work, but I've uploaded a screenshoot:
Edited by dxider: n/a
I ended up making this with codeorder's code and my code.
And ended up making a Visual Studio Add In with the same code.
It will count all lines of code in a folder.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click Dim FD As New FolderBrowserDialog If FD.ShowDialog = Windows.Forms.DialogResult.OK Then Dim WrittenCode, DesignerCode As Integer CountLinesofCodeInDirectory(FD.SelectedPath, WrittenCode, DesignerCode) End If End Sub Private Sub CountLinesofCodeInDirectory(ByVal DirectoryPath As String, ByRef TotalWritten As Integer, ByRef TotalDesigner As Integer) Try For Each File As String In System.IO.Directory.GetFiles(DirectoryPath) If File.EndsWith(".Designer.vb") Then TotalDesigner += System.IO.File.ReadAllLines(File).Count ElseIf File.EndsWith(".vb") Then TotalWritten += System.IO.File.ReadAllLines(File).Count End If Next For Each Directory As String In System.IO.Directory.GetDirectories(DirectoryPath) CountLinesofCodeInDirectory(Directory, TotalWritten, TotalDesigner) Next Catch ex As Exception End Try End Sub
So sad the thread is marked as solved, I made a new solution to include another project paths for multiple solution check, capabilities to choose another file extensions and a list with all files that matched the conditions with line count of each.
For the solution mail me at dxider at gmail.
Edited by dxider: n/a