This is vb.net extension method to find the total number of files in a folder. There is one exception. It will omit the system restricted file which can not be accessed. Recursively call itself to calculate subdirectories.

Code Snippet

    <Extension()> _
    Public Function GetTotalFiles(ByVal di As DirectoryInfo) As Int32

        Try

            Dim total As Int32
            total += di.GetFiles().Length

            Dim dis As DirectoryInfo() = di.GetDirectories()

            For Each d As DirectoryInfo In dis
                total += d.GetTotalFiles()
            Next

            Return total

        Catch
            Return 0
        End Try

    End Function

Sample Usage

Dim di As New DirectoryInfo("C://MyFolder")
Dim fileCount = di.GetTotalFiles()

Note

Need to import following to use this method

Imports System.Runtime.CompilerServices
Imports System.IO




author's photo

Author : Blue Cloud

Just a normal software developer who love to write code. Developing professional software since 1999. After working in the software industry for many years, I've started my own website to share knowledges and experiences.

COMMENTS
blog comments powered by Disqus