Reputation: 5
i made like this:
private void button4_Click(object sender, EventArgs e)
{
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Administrator\source\repos\Generator\Generator\bin\Debug\net5.0-windows");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
}
}
}
When i will click button4, show me all .txt from folder in listBox1. But when i add next one .txt in folder and click again, names are double. How to make that 1 file name = 1 position ?
Sorry for my english.
Upvotes: 0
Views: 46
Reputation: 22038
You should clear the listbox before re-adding the filenames.
private void button4_Click(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\Administrator\source\repos\Generator\Generator\bin\Debug\net5.0-windows");
FileInfo[] Files = dinfo.GetFiles("*.txt");
// Clear all previous items from the listbox
listBox1.Items.Clear();
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
}
}
Upvotes: 1