How to: Determine How Many Files Are in a Directory in Visual Basic
You can use the My.Computer.FileSystem.GetFiles Method to return a read-only collection of strings representing the names of files within the specified directory. Then you can use the Count property to determine the number of files.
To determine the number of files in a directory
Use the GetFiles method to return the collection of files in the specified directory. This example returns the files in the directory named TestDir.
Use the Count property to determine the number of files in the collection. This example displays the result in a message box.
This example, which presents the above snippet in complete form, counts the number of files in TestDir and reports it in a message box.
This example requires:
Access to the members of the System.Collections namespace. Add an Imports statement if you are not fully qualifying member names in your code. For more information, see Imports Statement (.NET Namespace and Type).
A directory named TestDir at the specified location. Replace the path with the path of the directory you wish to examine.
The following conditions may cause an exception:
The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
The directory does not exist or is a file (DirectoryNotFoundException).
directory points to an existing file (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or folder name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
The user lacks necessary permissions (UnauthorizedAccessException).