Export (0) Print
Expand All
9 out of 10 rated this helpful - Rate this topic

Directory.GetFiles Method (String)

Returns the names of files in the specified directory.

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

public static string[] GetFiles (
	string path
)
public static String[] GetFiles (
	String path
)
public static function GetFiles (
	path : String
) : String[]
Not applicable.

Parameters

path

The directory from which to retrieve the files.

Return Value

A String array of file names in the specified directory.
Exception typeCondition

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 (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
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);	    
    }
}

// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
import System.*;
import System.IO.*;
import System.Collections.*;

public class RecursiveFileProcessor
{
    public static void main(String[] args)
    {
        for (int iCtr = 0; iCtr < args.get_Length(); iCtr++) {
            String path = args[iCtr];
            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);
                }
            }
        }
    } //main

    // 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);
        for (int iCtr1 = 0; iCtr1 < fileEntries.get_Length(); iCtr1++) {
            String fileName = fileEntries[iCtr1];
            ProcessFile(fileName);
        }
        // Recurse into subdirectories of this directory.
        String subDirectoryEntries[] = 
            Directory.GetDirectories(targetDirectory);
        for (int iCtr2 = 0; iCtr2 < subDirectoryEntries.get_Length(); 
            iCtr2++) {
            String subDirectory = subDirectoryEntries[iCtr2];
            ProcessDirectory(subDirectory);
        }
    } //ProcessDirectory

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

//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());

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.