Number of files(Count) in folder - VB.Net


This article is written by Pon Saravanan  on 28-Aug-07 Last modified on :31-Aug-09





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











Comments
  • GUEST
    Thank you for this. 4/18/2010 6:27:14 AM

  • GUEST
    great 7/6/2011 5:15:21 AM

  • GUEST
    very nice sir 9/9/2011 5:45:32 AM

  • GUEST
    awesome 11/29/2012 4:40:58 PM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    



Spam Bot Trap



   



Select Theme
White
Blue
Brown
Gray