C# Directory EnumerateFiles(String)

Description

Directory EnumerateFiles(String) Returns an enumerable collection of file names in a specified path.

Syntax

Directory.EnumerateFiles(String) has the following syntax.


public static IEnumerable<string> EnumerateFiles(
  string path
)

Parameters

Directory.EnumerateFiles(String) has the following parameters.

  • path - The directory to search.

Returns

Directory.EnumerateFiles(String) method returns <

Example

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


//from   w w w.j av  a2  s. c o m

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);

        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