简体   繁体   English

如何获得目录中的所有扩展名?

[英]How can I able to get all the extensions in a Directory.?

How can I able to get all the extensions in a Directory. 如何获得目录中的所有扩展名。 This is for vb.net windows application. 这是用于vb.net Windows应用程序。

Thanks, Babu Kumarasamy. 谢谢,Babu Kumarasamy。

Get all files in the directory, get the extensions from them, and remove duplicates: 获取目录中的所有文件,从中获取扩展名,并删除重复项:

Dim extensions As String() = _
  Directory.GetFiles(path) _
  .Select(Function(f As String) Path.GetExtension(f)) _
  .Distinct() _
  .ToArray()

Edit: 编辑:
Changed to VB syntax 更改为VB语法

If you loop through all the files in a directory and take the extension of each file. 如果您遍历目录中的所有文件并采用每个文件的扩展名。 Check if it doesn't already exist in your list and if not add it. 检查列表中是否尚不存在,如果没有添加。 by the time you go through all the files you will have your list. 当您浏览所有文件时,您将拥有列表。

There isn't a single method to do this. 没有一种方法可以做到这一点。

I'd use a LINQ query with the VB.NET Group By Into syntax: 我将对VB.NET Group By Into语法使用LINQ查询:

Dim extensions = From file In New DirectoryInfo(path).GetFiles() _
                 Group file By file.Extension Into Group

You can then iterate through them like so: 然后,您可以像这样遍历它们:

For Each extension In extensions
    Console.WriteLine(extension.Extension)
Next

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

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