C# Directory GetFiles(String, String, SearchOption)

Description

Directory GetFiles(String, String, SearchOption) Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.

Syntax

Directory.GetFiles(String, String, SearchOption) has the following syntax.


public static string[] GetFiles(
  string path,//from   www  .ja va 2 s  . c  o m
  string searchPattern,
  SearchOption searchOption
)

Parameters

Directory.GetFiles(String, String, SearchOption) has the following parameters.

  • path - The directory to search.
  • searchPattern - The search string to match against the names of files in path. The parameter cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any of the characters in InvalidPathChars.
  • searchOption - One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.

Returns

Directory.GetFiles(String, String, SearchOption) method returns

Example


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

class Test 
{
    public static void Main() 
    {
        string[] dirs = Directory.GetFiles(@"c:\", "c*",SearchOption.AllDirectories);
        Console.WriteLine(dirs.Length);
        foreach (string dir in dirs) 
        {
            Console.WriteLine(dir);
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




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