C# Directory EnumerateFiles(String, String)

Description

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

Syntax

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


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

Parameters

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

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

Returns

Directory.EnumerateFiles(String, String) method returns <

Example

The following example shows how to retrieve all the text files in a directory and move them to a new directory.


// w w  w  .  ja v  a  2 s.c om
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string sourceDirectory = @"C:\current";
        string archiveDirectory = @"C:\archive";

        var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");

        foreach (string currentFile in txtFiles)
        {
            string fileName = currentFile.Substring(sourceDirectory.Length + 1);
            Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName));
        }
    }
}




















Home »
  C# Tutorial »
    System.IO »




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