c# 2005 - How do you populate a dropdownlist with filenames?

edmicman

Golden Member
May 30, 2001
1,682
0
0
I'm sure this is simple, but for the life of me everything I search google with doesn't come up with anything windows forms related, and the ASP.NET stuff I find is crap, too.

I'm in C#.NET 2005, and I want to populate a dropdown list with a list of files in the same directory as the executalbe. I want the full path as the ddl value, and just the filename as the ddl text. So far I have this: [hopefully it's attached?]

This is just putting the whole path as the SelectedItem of the combobox.....how do I do it like I want it?

 

Mark R

Diamond Member
Oct 9, 1999
8,513
16
81
Use the DirectoryInfo class to get a list of the files with and without their full paths.

FileInfo [] reportList = new DirectoryInfo (Directory.GetCurrentDirectory()).GetFiles ("*.rpt");

ComboBoxes take generic objects as their items and display their default string conversion. This means you can define a class which returns a default string (filename) and a value (full path).
 

edmicman

Golden Member
May 30, 2001
1,682
0
0
ahhhhhh, interesting - thanks! I found a sample on MSDN, but it was for ASP.NET, and I was having troubles figuring out the nuances between the two!