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***

Recommended Answers

All 3 Replies

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.

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

Gets all the extension in sorted order.

commented: Now I have to learn new C# features :D +8
commented: it was really useful +1
commented: nailed it with LINQ :) +14

Thank you so much for your answer
It helped my alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.