Bagikan melalui


My.Computer.FileSystem.GetFiles Method

Returns a read-only collection of strings representing the names of files within a directory.

' Usage
Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(directory)
Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(directory ,searchType ,wildcards)
' Declaration
Public Function GetFiles( _
   ByVal directory As String _
) As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
' -or-
Public Function GetFiles( _
   ByVal directory As String, _
   ByVal searchType As SearchOption, _
   ByVal wildcards As String() _
) As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

Parameters

  • directory
    String. Directory to be searched. Required.

  • searchType
    SearchOption Enumeration. Whether to include subfolders. Default is SearchOption.SearchTopLevelOnly. Required.

  • wildcards
    String. Pattern to be matched. Required.

Return Value

Read-only collection of strings.

Exceptions

The following conditions may cause an exception:

Remarks

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

Tasks

The following table lists examples of tasks involving the My.Computer.FileSystem.GetFiles method.

To

See

Get the collection of files in a directory

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

Find files with a specific pattern in a directory

How to: Find Files with a Specific Pattern in Visual Basic

Example

The following example returns all files in the directory and adds them to ListBox1.

For Each foundFile As String In My.Computer.FileSystem.GetFiles _
(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
    ListBox1.Items.Add(foundFile)
Next

This example requires that you have a ListBox named ListBox1 on your form.

This example returns all files in the directory with the extension .dll and adds them to ListBox1.

For Each foundFile As String In My.Computer.FileSystem.GetFiles _
      (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
       FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
    ListBox1.Items.Add(foundFile)
Next

This example requires that you have a ListBox named ListBox1 on your form.

Requirements

Namespace:Microsoft.VisualBasic.MyServices

Class:FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permission may be necessary:

Permission

Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Find Files with a Specific Pattern in Visual Basic

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

Reference

My.Computer.FileSystem Object

SearchOption Enumeration

System.Collections.ObjectModel.ReadOnlyCollection<T>

FileSystem.GetFiles