tags:

views:

34

answers:

3

Hi,

How can I able to get all the extensions in a Directory. This is for vb.net windows application.

Thanks, Babu Kumarasamy.

A: 

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.

Toby Allen
actually if you use a Set instead of a List you don't have to write any deduplication logic.
fuzzy lollipop
+3  A: 

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

Guffa
A: 

I'd use a LINQ query with the VB.NET Group By Into syntax:

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
Bermo