Export (0) Print
Expand All

Directory.GetFiles Method (String)

.NET Framework 1.1

Returns the names of files in the specified directory.

[Visual Basic]
Overloads Public Shared Function GetFiles( _
   ByVal path As String _
) As String()
[C#]
public static string[] GetFiles(
 string path
);
[C++]
public: static String* GetFiles(
 String* path
)  __gc[];
[JScript]
public static function GetFiles(
   path : String
) : String[];

Parameters

path
The directory from which to retrieve the files.

Return Value

A String array of file names in the specified directory.

Exceptions

Exception Type Condition
IOException path is a file name.
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 a null reference (Nothing in Visual Basic).
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, such as being on an unmapped drive.

Remarks

This method is identical to GetFiles(String, String) 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 path parameter is not case-sensitive.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Copy a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Rename or move a file. File.Move

FileInfo.MoveTo

Delete a directory. Directory.Delete

DirectoryInfo.Delete

Create a directory. CreateDirectory

Directory

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See the subdirectories of a directory. GetDirectories

GetDirectories

See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Determine if a file exists. Exists
Determine if a directory exists. Exists
Get file attributes. GetAttributes
Set file attributes. SetAttributes

Example

[Visual Basic] 
' 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

[C#] 
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }
        
    // Insert logic for processing found files here.
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);        
    }
}

[C++] 
// For Directory::GetFiles and Directory::GetDirectories
// For File::Exists, Directory::Exists
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Collections;

// Insert logic for processing found files here.
void ProcessFile(String* path) {
    Console::WriteLine(S"Processed file '{0}'.", path);        
}

// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
void ProcessDirectory(String* targetDirectory) {
    // Process the list of files found in the directory.
    String* fileEntries[] = Directory::GetFiles(targetDirectory);
    IEnumerator* files = fileEntries->GetEnumerator();
    while (files->MoveNext()) {
        String* fileName = __try_cast<String*>(files->Current);
        ProcessFile(fileName);
    }

    // Recurse into subdirectories of this directory.
    String* subdirectoryEntries[] = Directory::GetDirectories(targetDirectory);
    IEnumerator* dirs = subdirectoryEntries->GetEnumerator();
    while (dirs->MoveNext()) {
        String* subdirectory = __try_cast<String*>(dirs->Current);
        ProcessDirectory(subdirectory);
    }
}

int main(int argc, char* argv[]) {
    for(int i=1; i < argc; i++) {
        String* path = argv[i];
        if (File::Exists(path)) {
            // This path is a file
            ProcessFile(path); 
        } else if (Directory::Exists(path)) {
            // This path is a directory
            ProcessDirectory(path);
        }else {
            Console::WriteLine(S"{0} is not a valid file or directory.", path);
        }
    }
}        

[JScript] 
//For Directory.GetFiles and Directory.GetDirectories
import System;
import System.IO;
import System.Collections;

// For File.Exists, Directory.Exists 
// Takes an array of file names or directory names on the command line.  
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
    public static function Main(args : String[]) : void  {
        for(var i : int in args) {
            var path : String = args[i];
            if (File.Exists(path)) {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) {
                // This path is a directory
                ProcessDirectory(path);
            }
            else {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, and recurse on any directories 
    // that are found to process the files they contain
    public static function ProcessDirectory(targetDirectory : String) : void  {
        // Process the list of files found in the directory
        var fileEntries : String [] = Directory.GetFiles(targetDirectory);
        for (var i : int in fileEntries)
            ProcessFile(fileEntries[i]);

        // Recurse into subdirectories of this directory
        var subdirectoryEntries : String[] = Directory.GetDirectories(targetDirectory);
        for (i in subdirectoryEntries)
            ProcessDirectory(subdirectoryEntries[i]);
    }
        
    // Real logic for processing found files would go here.
    public static function ProcessFile(path : String) : void  {
        Console.WriteLine("Processed file '{0}'.", path);        
    }
}

// For JScript there is no 'Main' routine defined and hence the command line arguments
// have to be obtained with a call to System.Environment.GetCommandLineArgs
RecursiveFileProcessor.Main(System.Environment.GetCommandLineArgs());

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Directory Class | Directory Members | System.IO Namespace | Directory.GetFiles Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft