简体   繁体   English

获取文件扩展名以填充到C#中的ComboBox

[英]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: 所以我有一个包含许多扩展名的图像文件夹,如.ico.png.jpg等,我使用以下代码将其填充到一个comboBox

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: 代码获取此路径中的所有现有扩展并将其放在comboBox ,但它显示如下:

.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 . 您可以从FileInfo获取文件扩展名。 You can also use Linq Distinct() to get unique extensions. 您还可以使用Linq Distinct()来获取唯一的扩展名。

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?"). HashSet<T>让你回到这里:它允许快速查找以确定集合成员资格(“集合是否已包含某些元素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-to-Objects使这很容易。 LINQ is similar to SQL but allows chaining transformations. LINQ类似于SQL但允许链接转换。

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);

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

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