简体   繁体   English

给定文件夹(如 C:\Random 文件夹),如何找到所有扩展名并打印它们 C#?

[英]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.).我已经搜索并发现只有如何获得某个扩展名(如 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#System.IO如何找到给定文件夹的所有子文件夹? - c# System.IO how do i find all the subfolders given a certain folder? 如何在C#中找到具有指定值的文件夹? - How can I find a folder with a specified value in c#? 如何在 C# 中创建“递归”文件夹和子文件夹 - How can i create "recursively" folder and sub folder in C# C#,如何遍历所有驱动器和文件夹以复制图像并将它们粘贴到特定文件夹中? - C#, How to Traverse all drive and folder to copy image and paste them in a specific folder? (C#)如何读取文件夹中的所有文件以查找特定行? - (C#) How to read all files in a folder to find specific lines? 如何在C#中获取给定文件夹的组和用户名列表 - How can I get a list of Groups and User names for a given folder in C# 我如何给我的应用程序数据文件夹卡巴斯基像安全一样? - c# How can I give my app data folder Kaspersky like security? 如何读取所有当前进程并在控制台中打印它们? / C# ConsoleApp System.Diagnostics - How can I read all current processes and print them in console? / C# ConsoleApp System.Diagnostics 如何使用C#列出文件夹中的所有图像? - How do I list all images in a folder using C#? 如何使用打印预览从.net C#中的文件夹打印所有pdf文件,图像和docs文件? - How to Print all pdf file, Images and docs file from folder in .net C# with print preview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM