简体   繁体   中英

Get file extensions to populate into a ComboBox in C#

So I have a folder with some imagens in many extensions like .ico , .png , .jpg , etc. and I've populated it into a comboBox using this code:

string caminho = @"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
FileInfo[] fi = dir.GetFiles();
foreach (var ficheiro in fi)
{
    string caminhoF = caminho + ficheiro.ToString();
    string extension = Path.GetExtension(caminhoF);
    comboBox1.Items.Add(extension);
}

The code is getting all the existing extensions in this path and put it on the comboBox , but it displays like this:

.ico
.ico
.ico
.png
.png
.jpg
.jpg

and I want to simply display each one of the existing extensions like grouping them.

Could you help me with that?

You can get the file extension from the FileInfo . You can also use Linq Distinct() to get unique extensions.

string caminho = @"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
var extensions = dir.GetFiles().Select(fi => fi.Extension).Distinct();
foreach (var extension in extensions) {
    comboBox1.Items.Add(extension);
}

Ok, I was to find a solution for it. Here it is the code:

string caminho = @"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
FileInfo[] fi = dir.GetFiles();
foreach (var ficheiro in fi)
{
    string caminhoF = caminho + ficheiro.ToString();
    string extension = Path.GetExtension(caminhoF);
    if (!comboBox1.Items.Contains(extension))
    {
        comboBox1.Items.Add(extension);
    }
}

Here are the rough steps:

  • Scan your folder to find out what files it contains.
  • Extract the file extension from each file you find.
  • Using a data structure that stores only unique entries, add extensions that you find to be new to the structure.
  • Iterate over the data structure to populate your combobox.

The part that you need is to find a data structure that helps you store unique values.

HashSet<T> has your back here: it allows quick lookups to determine set membership ("does the set already contain some element x?").

string caminho = @"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
FileInfo[] fi = dir.GetFiles();
HashSet<string> extensions = new HashSet<string>;

foreach (var ficheiro in fi)
{
    string caminhoF = caminho + ficheiro.ToString();
    string extension = Path.GetExtension(caminhoF);

    // If the set does not contain this extension, it'll be added and
    // `Add()` will return true. Otherwise, it will do nothing and `Add()`
    // will return false.
    extensions.Add( extension );
}

foreach( var extension in extensions ) {
    comboBox1.Items.Add(extension);
}

LINQ-to-Objects makes this easy. LINQ is similar to SQL but allows chaining transformations.

var comboBox1 = new ComboBox();
var caminho = @"C:\Users\User1\Desktop\Test\";
var dir = new DirectoryInfo(caminho);
var extensions = dir.GetFiles()
       .Select(fi => fi.Extension)
       .OrderBy(ext => ext, StringComparer.CurrentCulture)
       .Distinct(StringComparer.CurrentCultureIgnoreCase)
       .ToArray();
comboBox1.Items.AddRange(extensions);

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