How to: Find Subdirectories with a Specific Pattern in Visual Basic
The GetDirectories method returns a read-only collection of strings representing the path names for the subdirectories in a directory. You can use the
wildCards parameter to specify a specific pattern. If you would like to include the contents of subdirectories in the search, set the
searchType parameter to
An empty collection is returned if no directories matching the specified pattern are found.
To find subdirectories with a specific pattern
GetDirectories method, supplying the name and path of the directory you want to search. The following example returns all the directories in the directory structure that contain the word "Logs" in their name, and adds them to
For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories( My.Computer.FileSystem.SpecialDirectories.MyDocuments, FileIO.SearchOption.SearchTopLevelOnly, "*Logs*") ListBox1.Items.Add(foundDirectory) Next
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
One or more of the specified wildcard characters is
Nothing, an empty string, or contains only spaces (ArgumentNullException).
directorydoes not exist (DirectoryNotFoundException).
directorypoints 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).