C# Directory GetDirectories(String, String, SearchOption)

Description

Directory GetDirectories(String, String, SearchOption) Gets the names of the subdirectories (including their paths) that match the specified search pattern in the current directory, and optionally searches subdirectories.

Syntax

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


public static string[] GetDirectories(
  string path,/*www .j a  va2s. c om*/
  string searchPattern,
  SearchOption searchOption
)

Parameters

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

  • path - The path to search.
  • searchPattern - The search string to match against the names of files in path. The parameter cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any of the characters in InvalidPathChars.
  • searchOption - One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.

Returns

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

Example

The following example counts the number of directories that begin with the specified letter in a path. Only the top-level directory is searched.


using System;/*from w  ww  .ja  v a 2 s .  c o  m*/
using System.IO;

class Test 
{
    public static void Main() 
    {
        string[] dirs = Directory.GetDirectories(@"c:\", "p*", SearchOption.TopDirectoryOnly);
        Console.WriteLine(dirs.Length);
        foreach (string dir in dirs) 
        {
            Console.WriteLine(dir);
        }
    }
}

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