VB.net Get Size Of Folder And Or A Drive PORTABLE
LINK === https://urlca.com/2tuUbS
How to get the size of a folder or a drive in VB.NET
In this article, we will learn how to use the System.IO namespace to get the size of a folder or a drive in VB.NET. The System.IO namespace provides various classes and methods for working with files and directories.
To get the size of a folder, we can use the DirectoryInfo class, which represents a directory in the file system. The DirectoryInfo class has a property called Length, which returns the size of the current directory in bytes. However, this property only includes the size of the files in the directory, not the subdirectories. To get the total size of the directory and its subdirectories, we need to use a recursive function that iterates through all the files and subdirectories and adds their sizes.
To get the size of a drive, we can use the DriveInfo class, which provides information about a drive. The DriveInfo class has a property called TotalSize, which returns the total size of the drive in bytes. Alternatively, we can use the TotalFreeSpace property to get the amount of free space available on the drive in bytes.
The following code snippet shows how to get the size of a folder or a drive in VB.NET:
'Imports System.IO namespace
Imports System.IO
'Function to get the size of a folder and its subfolders
Function GetFolderSize(ByVal path As String) As Long
'Create a DirectoryInfo object
Dim dir As New DirectoryInfo(path)
'Initialize a variable to store the size
Dim size As Long = 0
'Loop through all the files in the directory
For Each file As FileInfo In dir.GetFiles()
'Add the file size to the total size
size += file.Length
Next
'Loop through all the subdirectories in the directory
For Each subdir As DirectoryInfo In dir.GetDirectories()
'Recursively call the function to get the subdirectory size
size += GetFolderSize(subdir.FullName)
Next
'Return the total size
Return size
End Function
'Subroutine to display the size of a folder or a drive
Sub DisplaySize(ByVal path As String)
'Check if the path is a directory or a drive
If Directory.Exists(path) Then
'Call the function to get the folder size
Dim folderSize As Long = GetFolderSize(path)
'Convert the size to megabytes
Dim folderSizeMB As Double = folderSize / 1024 / 1024
'Display the result
Console.WriteLine(\"The size of {0} is {1:F2} MB\", path, folderSizeMB)
ElseIf DriveInfo.GetDrives().Any(Function(d) d.Name = path) Then
'Create a DriveInfo object
Dim drive As New DriveInfo(path)
'Get the drive size
Dim driveSize As Long = drive.TotalSize
'Convert the size to gigabytes
Dim driveSizeGB As Double = driveSize / 1024 / 1024 / 1024
'Display the result
Console.WriteLine(\"The size of {0} is {1:F2} GB\", path, driveSizeGB)
Else
'Display an error message
Console.WriteLine(\"Invalid path\")
End If
End Sub
'Sample usage
DisplaySize(\"C:\\Users\\Public\\Documents\")
DisplaySize(\"D:\\\")
DisplaySize(\"E:\\\")
The output of this code might look something like this:
The size of C:\\Users\\Public\\Documents is 123.45 MB
The size of D:\\ is 456.78 GB
Invalid path
We hope this article was helpful for you to learn how to get the size of a folder or a drive in VB.NET. Happy coding! ec8f644aee