0

Hi i have a directory containg about 300 files with different extensions like : .txt, .php, .html,etc.. and i have tried this code but it displays a files list instead of only thier extentions

string[] filePaths = Directory.GetFiles(@"c:\tmp]");

so what i need to do is to display all the files extensions in that folder

***Thank you***

3
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by shandoosheri
0

Once you have your files in a list you can loop through that list and use the getExtension method to return the file extension of each file. That will of course give you quite a few duplicates for each file type so you'd still need to filter that if you wanted a distinct list.

3
string[] fileExtensions = Directory.GetFiles(@"C:\temp").Select(p => Path.GetExtension(p)).Distinct().OrderBy(p => p).ToArray();

Gets all the extension in sorted order.

Edited by Momerath: n/a

Votes + Comments
nailed it with LINQ :)
it was really useful
Now I have to learn new C# features :D
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.