Skip to content

Latest commit

 

History

History
50 lines (31 loc) · 3 KB

how-to-get-the-collection-of-files-in-a-directory.md

File metadata and controls

50 lines (31 loc) · 3 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: How to: Get the Collection of Files in a Directory in Visual Basic
How to: Get the Collection of Files in a Directory
07/20/2015
folders, working with
files [Visual Basic], accessing
6c8ba7e8-dd37-4853-92bf-762b67c98160

How to: Get the Collection of Files in a Directory in Visual Basic

The overloads of the xref:Microsoft.VisualBasic.FileIO.FileSystem.GetFiles%2A?displayProperty=nameWithType method return a read-only collection of strings representing the names of the files within a directory:

  • Use the xref:Microsoft.VisualBasic.FileIO.FileSystem.GetFiles%28System.String%29 overload for a simple file search in a specified directory, without searching subdirectories.

  • Use the xref:Microsoft.VisualBasic.FileIO.FileSystem.GetFiles(System.String,Microsoft.VisualBasic.FileIO.SearchOption,System.String[]) overload to specify additional options for your search. You can use the wildCards parameter to specify a search pattern. To include subdirectories in the search, set the searchType parameter to xref:Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories?displayProperty=nameWithType.

An empty collection is returned if no files matching the specified pattern are found.

To list files in a directory

  • Use one of the xref:Microsoft.VisualBasic.FileIO.FileSystem.GetFiles%2A?displayProperty=nameWithType method overloads, supplying the name and path of the directory to search in the directory parameter. The following example returns all files in the directory and adds them to ListBox1.

    [!code-vbVbVbcnMyFileSystem#32]

Robust Programming

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 \\.\) (xref:System.ArgumentException).

  • The path is not valid because it is Nothing (xref:System.ArgumentNullException).

  • directory does not exist (xref:System.IO.DirectoryNotFoundException).

  • directory points to an existing file (xref:System.IO.IOException).

  • The path exceeds the system-defined maximum length (xref:System.IO.PathTooLongException).

  • A file or directory name in the path contains a colon (:) or is in an invalid format (xref:System.NotSupportedException).

  • The user lacks necessary permissions to view the path (xref:System.Security.SecurityException).

  • The user lacks necessary permissions (xref:System.UnauthorizedAccessException).

See also