Sample Program to get file count of a folder(VB.Net)
In Vb.Net counting files in a Directory is easier than before. it can be done in a single line.
The following function can be placed in a module or class file. And the same can be used from the other modules like
Msgbox GetFileCount(
"c:\")
'******************************************************************************
'*Purpose : Get File Count Using VB.Net
'*Inputs: strPath(string) : Path of the folder.
'*
'*Returns : File Count
'******************************************************************************
Public Function GetFileCount(ByVal strPath As String) As Integer
Try
Return System.IO.Directory.GetFiles(strPath).Length()
Catch ex As Exception
Throw New Exception("Error From GetFileCount Function" & ex.Message, ex)
End Try
End Function
Are you looking for Classic Visual Basic (VB6) version? Get number of files in a folder(file count) using Classic VB
To count files in the sub directories
In Vb.Net counting files in a Directory including subfolders is very easier and does not need any recursion by developer. there is a parameter to the GetFiles() function to include teh sub folder content inside. The parameter name is SearchOption.AllDirectories
System.IO.Directory.GetFiles("d:\pons","*.*",IO.SearchOption.AllDirectories).Length