简体   繁体   中英

A given folder (like C:\Random Folder), how can I find ALL the extensions and print them C#?

I've searched and found only how to get a certain extension(like txt.). I need to get all of the extensions and print them on the console. How is that possible?

var dir = new DirectoryInfo(@"C:\Random\");
var allExtensions = dir.EnumerateFiles().Select(f => f.Extension).Distinct();
foreach (string ext in allExtensions)
    Console.WriteLine(ext);

Use this if you want to list all extensions also in sub-directories:

var allExtensions = dir.EnumerateFiles("*.*", SearchOption.AllDirectories)
    .Select(f => f.Extension)
    .Distinct();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM