C# Directory EnumerateDirectories(String, String)

Description

Directory EnumerateDirectories(String, String) Returns an enumerable collection of directory names that match a search pattern in a specified path.

Syntax

Directory.EnumerateDirectories(String, String) has the following syntax.


public static IEnumerable<string> EnumerateDirectories(
  string path,
  string searchPattern
)

Parameters

Directory.EnumerateDirectories(String, String) has the following parameters.

  • path - The directory to search.
  • searchPattern - The search string to match against the names of directories in path.

Returns

Directory.EnumerateDirectories(String, String) method returns <

Example

The following example enumerates the top-level directories in a specified path that match a specified search pattern.


using System;//from w ww .  j  a  va 2s. c  o  m
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Program
{
    private static void Main(string[] args)
    {
        string dirPath = @"\\archives";

        var dirs = from dir in Directory.EnumerateDirectories(dirPath, "*")
                   select dir;

        foreach (var dir in dirs)
        {
            Console.WriteLine("{0}", 
                dir.Substring(dir.LastIndexOf("\\") + 1));

        }
        Console.WriteLine(dirs.Count<string>().ToString());

        List<string> workDirs = new List<string>(dirs);
    }
}




















Home »
  C# Tutorial »
    System.IO »




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