VB.Net program to get the size of a specified folder including a sub-folder

Here, we are going to learn how to get the size of a specified folder including a sub-folder in VB.Net?
Submitted by Nidhi, on January 06, 2021

Here, we will use DirectoryInfo class to get the size of the folder and its sub-folder in GB and print the result on the console screen.

Program/Source Code:

The source code to get the size of a specified folder including the sub-folder is given below. The given program is compiled and executed successfully.

'Vb.Net program to demonstrate memory stream class.

Imports System.IO
Imports System.Linq

Module Module1
    Function GetSize(ByVal dInfo As DirectoryInfo, ByVal isSubFolder As Boolean) As Long
        Dim sizeInBytes As Long = 0

        sizeInBytes = dInfo.EnumerateFiles().Sum(Function(fi) fi.Length)

        If isSubFolder = True Then
            sizeInBytes += dInfo.EnumerateDirectories().Sum(Function(Dir) GetSize(Dir, True))
        End If
        Return sizeInBytes
    End Function
    Sub Main()
        Dim sizeOfDir As Long
        Dim sizeGb As Double

        Dim dInfo As New DirectoryInfo("C:/Intel")
        sizeOfDir = GetSize(dInfo, True)
        sizeGb = (sizeOfDir) / (1024 * 1024 * 1024)

        Console.WriteLine("Size of folder including sub-folder in GB: " & sizeGb)
    End Sub
End Module


Size of folder including sub-folder in GB: 18.12728
Press any key to continue . . .


In the above program, we created a class module Module1 that a Main() function. The Main() method is the entry point of the program, Here, we created a class Demo that contains two static methods GetSize() and Main(). Here we get the size of the specified folder including the sub-folder using DirectoryInfo class with LINQ Sum() and EnumerateFiles() methods. It returns the size of the folder in bytes then we converted the number of bytes into GB by dividing by (1024*1024*1024), then print size into gigabytes on the console screen.

VB.Net StringReader, StringWriter, Stream Programs »


Comments and Discussions!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.