How to Get the Size of the Directory in VB.NET
The .NET framework comes with a class library that provides access to Windows system functions. Developers instantiate classes and reference functions within the classes in their applications. This class library provides several benefits, such as faster development, easier reuse of code, increased scalability and easier deployment. The Directory class is one of the class libraries available in .NET that is used to access files and directories.
Instructions
-
Using the Directory Class in .NET
-
1
Type "Imports System.IO" in the "declarations" section. This allows your program to access the Directory class functions.
-
2
Create a function that will perform the folder size computation. Name it something that appropriately describes what the function does. Pass the directory name to this function so that it knows where to look for the files to count. Declare variables within this function of type Long, FileInfo and DirectoryInfo.
-
-
3
Get the length of each file in the specified directory. Add the length of each file to the variable of type Long that will be used to store the total size of all files. The command used to get the length of a file is "FileInfo.Length."
-
4
Count all the files, including subdirectories, by calling "DirectoryInfo.GetDirectories" and then referencing this same function, passing the name of the subdirectory. This will force the function to call itself and get the length of each file in the particular subdirectory name. The total will be accumulated in that Long variable defined earlier that keeps a tally of the length of each file.
-
1
References
- Photo Credit Hemera Technologies/PhotoObjects.net/Getty Images