Nathan Anderson
Nathan Anderson

Reputation: 19

C# fill listbox with combobox selected hard drive's contents

I've been wandering how to do this for weeks now, I have a combobox which lists available local hard drives, I can get that far but below that is a listbox, I want the list box to be filled with contents from the selected hard drive dependent on which hdd was selected in the combo box), I not looking for the entire code to be wrote out for me but would like if someone can steer me in a starting direction, much appreciated.

Upvotes: 0

Views: 1315

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34844

This will get you going in the right direction as for the objects involved to do what you want to do:

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

Usage:

PopulateListBox(listbox1, @"C:\Files", "*.pdf");
PopulateListBox(listbox2, @"C:\Files", "*.doc");

Upvotes: 1

Related Questions