Directory.GetFiles Method (String)
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| path is a file name. | |
| The caller does not have the required permission. | |
| path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars. | |
| path is a null reference (Nothing in Visual Basic). | |
| 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. | |
| The specified path is invalid (for example, it is on an unmapped drive). |
The file names include the full path.
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 path parameter is not case-sensitive.
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. | |
| Write to a text file. | |
| Read from a text file. | |
| Rename or move a directory. | |
| Rename or move a file. | |
| Delete a directory. | |
| Create a directory. | |
| Create a subdirectory. | |
| See the files in a directory. | |
| See the subdirectories of a directory. | |
| See all the files in all subdirectories of a directory. | |
| Find the size of a directory. | |
| Determine if a file exists. | |
| Determine if a directory exists. | |
| Get file attributes. | |
| Set file attributes. |
// For Directory::GetFiles and Directory::GetDirectories // For File::Exists, Directory::Exists using namespace System; using namespace System::IO; using namespace System::Collections; // Insert logic for processing found files here. void ProcessFile( String^ path ) { Console::WriteLine( "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. array<String^>^fileEntries = Directory::GetFiles( targetDirectory ); IEnumerator^ files = fileEntries->GetEnumerator(); while ( files->MoveNext() ) { String^ fileName = safe_cast<String^>(files->Current); ProcessFile( fileName ); } // Recurse into subdirectories of this directory. array<String^>^subdirectoryEntries = Directory::GetDirectories( targetDirectory ); IEnumerator^ dirs = subdirectoryEntries->GetEnumerator(); while ( dirs->MoveNext() ) { String^ subdirectory = safe_cast<String^>(dirs->Current); ProcessDirectory( subdirectory ); } } int main( int argc, char *argv[] ) { for ( int i = 1; i < argc; i++ ) { String^ path = gcnew String(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( "{0} is not a valid file or directory.", 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());
- FileIOPermission for access to path information for the current directory. Associated enumeration: FileIOPermissionAccess.PathDiscovery
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.