Saturday, 11 January 2014

Get Files from Directory - C#

Leave a Comment
This example 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 „c:\MyDir“ folder:

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

using
System.IO;

string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\my-house.jpg"


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

string [] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");
// returns:
// "c:\MyDir\my-car.BMP"

Get files from directory (including all subdirectories) If you want to search also in subfolders use parameter SearchOption.A­llDirectories.

string [] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp",
                                         SearchOption.AllDirectories);
// returns:
// "c:\MyDir\my-car.BMP"
// "c:\MyDir\Friends\james.BMP"


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.