将文件从 C# windows 窗体中的文件夹填充到列表框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10516167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate files into a listbox from a folder in C# windows forms
提问by linguini
I'm a newbie in C# and I have 2 Listboxes l-->istBox1 and listBox2 and I want to load files from folder into these listboxes.
I tried like this :
listBox1:
我是 C# 的新手,我有 2 个列表框 l-->istBox1 和 listBox2,我想将文件夹中的文件加载到这些列表框中。我试过这样:
listBox1:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.rtdl");
foreach (FileInfo file in Files)
{
listbox1.Items.Add(file.Name);
}
}
listBox2:
listBox2:
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.dlz");
foreach (FileInfo file in Files)
{
listbox2.Items.Add(file.Name);
}
}
when i run the form, the files from the folder is not displaying???
当我运行表单时,文件夹中的文件没有显示???
采纳答案by Habib
Instead of listBox1_SelectedIndexChanged, update the listbox against some button click, otherwise your code looks fine. Initially you probably don't have any item in your listbox and that's why SelectedIndexChanged doesn't get fired when you click on it.
而不是 listBox1_SelectedIndexChanged,针对某些按钮单击更新列表框,否则您的代码看起来不错。最初,您的列表框中可能没有任何项目,这就是单击它时 SelectedIndexChanged 不会被触发的原因。
Edit: (Since the question has been edited, I will update my answer)
To pouplate your listboxes with Files, you should do that, in some event other than SelectedIndexChanged. Because at the start of your application your listboxes are empty and SelectedIndexChanged event gets fired when there are items in the listbox and user click on it. You may create the following function
编辑:(由于问题已被编辑,我将更新我的答案)
要使用文件填充列表框,您应该这样做,在 SelectedIndexChanged 以外的某些情况下。因为在您的应用程序开始时,您的列表框是空的,并且当列表框中有项目并且用户单击它时,会触发 SelectedIndexChanged 事件。您可以创建以下函数
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
Now you may call this function with your listbox in some event against a button click or form load. e.g.
现在,您可以在某些事件中针对按钮单击或表单加载使用列表框调用此函数。例如
private void Form1_Load(object sender, EventArgs e)
{
PopulateListBox(listbox1, @"C:\TestLoadFiles", "*.rtld");
PopulateListBox(listbox2, @"C:\TestLoadFiles", "*.other");
}
回答by Not loved
This might work ;)
这可能有效;)
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.rtdl");
foreach (FileInfo file in Files)
{
listbox2.Items.Add(file.Name);
}
}
回答by Till
Wrong event i suppose. Move that code to the constructor of your form/control or attach it to an event of another control. Repopulating the listBox on SelectedIndexChanged when the initial state of the listbox is empty does not make sense.
我想是错误的事件。将该代码移动到窗体/控件的构造函数或将其附加到另一个控件的事件。当列表框的初始状态为空时,在 SelectedIndexChanged 上重新填充列表框没有意义。

