C# DirectoryInfo GetDirectories(String)

Description

DirectoryInfo GetDirectories(String) Returns an array of directories in the current DirectoryInfo matching the given search criteria.

Syntax

DirectoryInfo.GetDirectories(String) has the following syntax.


public DirectoryInfo[] GetDirectories(
  string searchPattern
)

Parameters

DirectoryInfo.GetDirectories(String) 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".

Returns

DirectoryInfo.GetDirectories(String) method returns

Example

The following example counts the directories in a path that contain the specified letter.


using System;/*from   ww  w . j a  v  a 2s .  com*/
using System.IO;

class Test 
{
    public static void Main() 
    {
        DirectoryInfo di = new DirectoryInfo(@"c:\");
        DirectoryInfo[] dirs = di.GetDirectories("*p*");
        Console.WriteLine(dirs.Length);
        foreach (DirectoryInfo diNext in dirs) 
        {
            Console.WriteLine("The number of files in {0} is {1}", diNext, 
                diNext.GetFiles().Length);
        }
    }
}

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