C# DirectoryInfo GetDirectories(String, SearchOption)

Description

DirectoryInfo GetDirectories(String, SearchOption) Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

Syntax

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


public DirectoryInfo[] GetDirectories(
  string searchPattern,
  SearchOption searchOption
)

Parameters

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

  • searchPattern - The search string. For example, "System*" can be used to search for all directories that begin with the word "System".
  • searchOption - One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories.

Returns

DirectoryInfo.GetDirectories(String, SearchOption) method returns

Example

The following example lists all of the directories and files that begin with the letter "c" in "c:\".


/* w  w w .  j a  va2 s  .co  m*/
using System;
using System.IO;

class App
{
    public static void Main()
    {
        string path = @"c:\";
        string searchPattern = "c*";

        DirectoryInfo di = new DirectoryInfo(path);
        DirectoryInfo[] directories = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
        FileInfo[] files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);

        Console.WriteLine(path);
        foreach (DirectoryInfo dir in directories)
        {
            Console.WriteLine(dir.FullName);
        }

        foreach (FileInfo file in files)
        {
            Console.WriteLine(
                "{0,-25} {1,25}", file.Name, file.LastWriteTime);
        }
    }
}

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