Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Directory.GetFiles Method (String)

Returns the names of files (including their paths) in the specified directory.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function GetFiles ( _
	path As String _
) As String()
'Usage
Dim path As String 
Dim returnValue As String()

returnValue = Directory.GetFiles(path)

Parameters

path
Type: System.String

The directory from which to retrieve the files.

Return Value

Type: System.String()
A String array of file names in the specified directory. File names include the full path.

ExceptionCondition
IOException

path is a file name.

-or-

A network error has occurred.

UnauthorizedAccessException

The caller does not have the required permission.

ArgumentException

path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

ArgumentNullException

path is Nothing.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

DirectoryNotFoundException

The specified path is invalid (for example, it is on an unmapped drive).

The returned file names are appended to the supplied path parameter.

If there are no files, this method returns an empty array.

This method is identical to GetFiles with the asterisk (*) specified as the search pattern.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.

The path parameter is not case-sensitive.

For a list of common I/O tasks, see Common I/O Tasks.

The following example demonstrates how to use the GetFiles method to return file names from a user-specified location. The example is configured to catch all errors common to this method.

' For Directory.GetFiles and Directory.GetDirectories 
' For File.Exists, Directory.Exists  

Imports System
Imports System.IO
Imports System.Collections

Public Class RecursiveFileProcessor

    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim path As String 
        For Each path In args
            If File.Exists(path) Then 
                ' This path is a file.
                ProcessFile(path)
            Else 
                If Directory.Exists(path) Then 
                    ' This path is a directory.
                    ProcessDirectory(path)
                Else
                    Console.WriteLine("{0} is not a valid file or directory.", path)
                End If 
            End If 
        Next path
    End Sub 'Main


    ' Process all files in the directory passed in, recurse on any directories  
    ' that are found, and process the files they contain. 
    Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory. 
        Dim fileName As String 
        For Each fileName In fileEntries
            ProcessFile(fileName)

        Next fileName
        Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
        ' Recurse into subdirectories of this directory. 
        Dim subdirectory As String 
        For Each subdirectory In subdirectoryEntries
            ProcessDirectory(subdirectory)
        Next subdirectory

    End Sub 'ProcessDirectory

    ' Insert logic for processing found files here. 
    Public Shared Sub ProcessFile(ByVal path As String)
        Console.WriteLine("Processed file '{0}'.", path)
    End Sub 'ProcessFile
End Class 'RecursiveFileProcessor

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

Show:
© 2017 Microsoft