C# Get All Files from a Folder | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

C# Get All Files from a Folder

Tuesday

C# Get All Files from a Folder:
                                                This post shows how to get list of file names from a directory (including subdirectories). You can filter the list by specific extension.
To get file names from the specified directory, use static method Directory.Get­Files. Lets have these files and subfolders in "d:\YourDir" folder:

Get files from directory
Method Directory.GetFiles returns string array with files names (full paths).

C# Code
using System.IO;
string[] filePaths = Directory.GetFiles(@"d:\Images\");
// returns:
// "d:\Images\codingcluster.png"
// "d:\Images\home.jpg"


Get files from directory (with specified extension)
You can specify search pattern. You can use wildcard specifiers in the search pattern, e.g. „*.png“ to select files with the extension or „a*“ to select files beginning with letter, a“.

C# Code
string[] filePaths = Directory.GetFiles(@"d:\Images\", "*.png");
// returns:
// "c:\Images\codingcluster.png"

Get files from directory (including all sub directories)
If you want to search also in subfolders use parameter Search Option. A­ll Directories.

C# Code
string[] filePaths = Directory.GetFiles(@"d:\Images\", "*.png",
                                         SearchOption.AllDirectories);
// returns:
// "d:\Images\dcodingcluster.png"
// "d:\Images\Posts\header.png"

Share this post if it is useful to you. Thanks!.

0 comments:

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets