Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual Basic
 GetFiles Method (My.Computer.FileSy...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Basic Language Reference
My.Computer.FileSystem.GetFiles Method

Updated: November 2007

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)
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.

Read-only collection of strings.

The following conditions may cause an exception:

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

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

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

Visual Basic
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.

Visual Basic
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.

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

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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker