Directory::GetFileSystemEntries Method (String^, String^)
Returns an array of file names and directory names that that match a search pattern in a specified path.
Assembly: mscorlib (in mscorlib.dll)
public: static array<String^>^ GetFileSystemEntries( String^ path, String^ searchPattern )
Parameters
- path
-
Type:
System::String^
The relative or absolute path to the directory to search. This string is not case-sensitive.
- searchPattern
-
Type:
System::String^
The search string to match against the names of file and directories in path. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions.
Return Value
Type: array<System::String^>^An array of file names and directory names that match the specified search criteria, or an empty array if no files or directories are found.
| Exception | Condition |
|---|---|
| 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. You can query for invalid characters with the GetInvalidPathChars method. -or- searchPattern does not contain a valid pattern. |
| ArgumentNullException | path or searchPattern is null. |
| 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. |
| IOException | path is a file name. |
| DirectoryNotFoundException | The specified path is invalid (for example, it is on an unmapped drive). |
The order of the returned file and directory names is not guaranteed; use the Sort method if a specific sort order is required.
searchPattern can be a combination of literal and wildcard characters, but doesn't support regular expressions. The following wildcard specifiers are permitted in searchPattern.
Wildcard specifier | Matches |
|---|---|
* (asterisk) | Zero or more characters in that position. |
? (question mark) | Zero or one character in that position. |
Characters other than the wildcard are literal characters. For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".
searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.
Note |
|---|
When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:
When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files. |
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 a list of common I/O tasks, see Common I-O Tasks.
The following example uses the GetFileSystemEntries method to fill an array of strings with the names of all files matching a user-specified filter in a specific location and prints each string in the array to the console. The example is configured to catch all errors common to this method.
using namespace System; class Class1 { public: void PrintFileSystemEntries( String^ path ) { try { // Obtain the file system entries in the directory path. array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path ); for ( int i = 0; i < directoryEntries->Length; i++ ) { System::Console::WriteLine( directoryEntries[ i ] ); } } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." ); } catch ( System::IO::DirectoryNotFoundException^ ) { System::Console::WriteLine( "The path encapsulated in the \HelloServer' Directory object does not exist." ); } } void PrintFileSystemEntries( String^ path, String^ pattern ) { try { // Obtain the file system entries in the directory // path that match the pattern. array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path, pattern ); for ( int i = 0; i < directoryEntries->Length; i++ ) { System::Console::WriteLine( directoryEntries[ i ] ); } } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." ); } catch ( System::IO::DirectoryNotFoundException^ ) { System::Console::WriteLine( "The path encapsulated in the \HelloServer' Directory object does not exist." ); } } // Print out all logical drives on the system. void GetLogicalDrives() { try { array<String^>^drives = System::IO::Directory::GetLogicalDrives(); for ( int i = 0; i < drives->Length; i++ ) { System::Console::WriteLine( drives[ i ] ); } } catch ( System::IO::IOException^ ) { System::Console::WriteLine( "An I/O error occurs." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } } void GetParent( String^ path ) { try { System::IO::DirectoryInfo^ directoryInfo = System::IO::Directory::GetParent( path ); System::Console::WriteLine( directoryInfo->FullName ); } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, or \HelloServer' contains invalid characters." ); } } void Move( String^ sourcePath, String^ destinationPath ) { try { System::IO::Directory::Move( sourcePath, destinationPath ); System::Console::WriteLine( "The directory move is complete." ); } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." ); } catch ( System::IO::IOException^ ) { System::Console::WriteLine( "An attempt was made to move a \HelloServer' directory to a different \HelloServer' volume, or destDirName \HelloServer' already exists." ); } } }; int main() { Class1 * snippets = new Class1; String^ path = System::IO::Directory::GetCurrentDirectory(); String^ filter = "*.exe"; snippets->PrintFileSystemEntries( path ); snippets->PrintFileSystemEntries( path, filter ); snippets->GetLogicalDrives(); snippets->GetParent( path ); snippets->Move( "C:\\proof", "C:\\Temp" ); return 0; }
for access to path information for the current directory. Associated enumeration: FileIOPermissionAccess::PathDiscovery
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
