C# Directory GetFileSystemEntries(String, String, SearchOption)

Description

Directory GetFileSystemEntries(String, String, SearchOption) Gets an array of all the file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.

Syntax

Directory.GetFileSystemEntries(String, String, SearchOption) has the following syntax.


public static string[] GetFileSystemEntries(
  string path,/* ww w.  j  a  v a 2  s  .  co m*/
  string searchPattern,
  SearchOption searchOption
)

Parameters

Directory.GetFileSystemEntries(String, String, SearchOption) has the following parameters.

  • path - The directory to search.
  • searchPattern - The string used to search for all files or directories that match its search pattern. The default pattern is for all files and directories: "*"
  • searchOption - One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories.
  • searchOption - The default value is TopDirectoryOnly.

Returns

Directory.GetFileSystemEntries(String, String, SearchOption) method returns

Example

Gets an array of all the file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.


using System;//from   www.  j  a  v  a 2s . c om
using System.IO;

class Class1
{
    static void Main(string[] args)
    {
        string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(".", "*a", SearchOption.AllDirectories);

        foreach (string str in directoryEntries)
        {
            System.Console.WriteLine(str);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter