C# Directory GetFiles(String, String)

Description

Directory GetFiles(String, String) Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

Syntax

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


public static string[] GetFiles(
  string path,
  string searchPattern
)

Parameters

Directory.GetFiles(String, String) 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.

Returns

Directory.GetFiles(String, String) method returns

Example

The following example counts the number of files that begin with the specified letter.


/*from   ww  w .  ja  v  a  2s .  c o m*/
using System;
using System.IO;

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

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