C# Directory EnumerateDirectories(String, String, SearchOption)
Description
Directory EnumerateDirectories(String, String, SearchOption)
Returns an enumerable collection of directory names that match
a search pattern in a specified path, and optionally searches subdirectories.
Syntax
Directory.EnumerateDirectories(String, String, SearchOption)
has the following syntax.
public static IEnumerable<string> EnumerateDirectories(
string path,//from w w w .ja va 2s . c o m
string searchPattern,
SearchOption searchOption
)
Parameters
Directory.EnumerateDirectories(String, String, SearchOption)
has the following parameters.
path
- The directory to search.searchPattern
- The search string to match against the names of directories in path.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.EnumerateDirectories(String, String, SearchOption)
method returns <
Example
The following example enumerates directories in a specified path that match a specified search pattern. It uses the SearchOption parameter to specify that all subdirectories should be included in the search.
using System;//from ww w . j a v a 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, "*",
SearchOption.AllDirectories)
select dir;
foreach (var dir in dirs)
{
Console.WriteLine("{0}",
dir.Substring(dir.LastIndexOf("\\") + 1));
}
// Optionally create a List collection.
List<string> workDirs = new List<string>(dirs);
}
}