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

By Nidhi Last Updated : November 15, 2024

Getting the size of a folder including a sub-folder in VB.Net

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 code to get the size of a specified folder including a sub-folder

'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

Output

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

Explanation

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 »


Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.