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

Dani AI

Generated

Quick notes and a slightly more robust approach.

The original path in Post #1 looks mistyped (c:\tmp]) — that will throw an error before you even get to extensions. As pointed out, use Path.GetExtension to get each file's extension, and the one‑liner from is a fine quick solution. For production or edge cases (case differences, dotfiles, compound extensions like .tar.gz, very large folders or permission errors) the following expands on those ideas.

// enumerates safely, normalizes and handles a few edge cases
using System.IO;
using System.Linq;
using System.Collections.Generic;

string folder = @"C:\temp";
var knownCompound = new[] { ".tar.gz", ".tar.bz2", ".tar.xz", ".d.ts", ".min.js" };
var extSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

try
{
    foreach (var f in Directory.EnumerateFiles(folder, "*", SearchOption.TopDirectoryOnly))
    {
        var name = Path.GetFileName(f);

        var compound = knownCompound.FirstOrDefault(c => name.EndsWith(c, StringComparison.OrdinalIgnoreCase));
        if (compound != null) { extSet.Add(compound.TrimStart('.')); continue; }

        var ext = Path.GetExtension(name); // includes leading dot, or empty
        if (string.IsNullOrEmpty(ext))
        {
            // treat dotfiles (".gitignore") or explicitly mark "none"
            if (name.StartsWith(".") && name.LastIndexOf('.') == 0) extSet.Add(name);
            else extSet.Add("(none)");
            continue;
        }

        extSet.Add(ext.TrimStart('.').ToLowerInvariant());
    }

    var sorted = extSet.OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToArray();
}
catch (DirectoryNotFoundException) { /* validate path */ }
catch (UnauthorizedAccessException) { /* permissions */ }

Practical tips: use Directory.EnumerateFiles for large sets (streams results), use a case‑insensitive comparer to avoid ".TXT" vs ".txt" duplicates, and keep a small list of known compound extensions if you need them. For a quick throwaway script the LINQ one‑liner is fine; for production, handle exceptions and the dotfile/compound cases shown above.

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.