C# Directory EnumerateFileSystemEntries(String, String, SearchOption)
Description
Directory EnumerateFileSystemEntries(String, String, SearchOption)
Returns an enumerable collection of file names and directory names
that match a search pattern in a specified path, and optionally searches
subdirectories.
Syntax
Directory.EnumerateFileSystemEntries(String, String, SearchOption)
has the following syntax.
public static IEnumerable<string> EnumerateFileSystemEntries(
string path,// w w w .j av a2s . c o m
string searchPattern,
SearchOption searchOption
)
Parameters
Directory.EnumerateFileSystemEntries(String, String, SearchOption)
has the following parameters.
path
- The directory to search.searchPattern
- The search string to match against the file-system entries 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.EnumerateFileSystemEntries(String, String, SearchOption)
method returns <
Example
Returns an enumerable collection of file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.
/*from www. j ava 2 s .c o m*/
using System;
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.EnumerateFileSystemEntries(dirPath, "*",
SearchOption.AllDirectories)
select dir;
foreach (var dir in dirs)
{
Console.WriteLine("{0}",
dir.Substring(dir.LastIndexOf("\\") + 1));
}
List<string> workDirs = new List<string>(dirs);
}
}