.

Monday, January 5, 2009

Get All Files from a Folder in C#.Net

The following code lists all the files from the specifed folder and its sub folders

To list all Excel files using C#, modify the first argument to “*.xls”

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.AllDirectories );

foreach (FileInfo FI in FileList )

{

Console.WriteLine(FI.FullName);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}

The following wildcard specifiers are permitted in searchPattern.

Wildcard character

Description

*

Zero or more characters.

?

Exactly one character.

Search for Word files starting with “Proposal” using C# in Current Directory

FileInfo[] FileList = Dir.GetFiles("Employee*.xls", SearchOption.TopDirectoryOnly );


No comments:

.