Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Can i boot combobox with the names of all the files on my computer?
Or at least the names of the files are located in the project folder?
Posted
Updated 2-Oct-13 0:11am
v3
Comments
tanweer 2-Oct-13 6:02am    
do you want to load all files in your dropdownlist?

 
Share this answer
 
Comments
Member 10304617 2-Oct-13 7:52am    
Thanks.
Yes: the simplest way is to handle the Form.Shown event and add this:
C#
string[] files = Directory.GetFiles(@"C:\MyProjects\", "*.*", SearchOption.AllDirectories);
myComboBox.Items.AddRange(files);
That adds the entire path, so if you want to add just the files names you will have to use a little Linq method:
C#
string[] files = Directory.GetFiles(@"C:\MyProjects\", "*.*", SearchOption.AllDirectories);
var namesOnly = files.Select(f => Path.GetFileNameWithoutExtension(f)).ToArray();
myComboBox.Items.AddRange(namesOnly);
 
Share this answer
 
Comments
Member 10304617 2-Oct-13 6:34am    
Thanks... you help me a lot
OriginalGriff 2-Oct-13 6:38am    
You're welcome!
Member 10304617 2-Oct-13 6:45am    
I do it for 2 combobox, how can I reduce the selected item of the first combobox?
from the second combobox
OriginalGriff 2-Oct-13 6:55am    
Sorry? That doesn't make any sense to me - remember I can't see your screen, so I have no idea what you are trying to do! :laugh:
Try explaining in more detail, please.
Member 10304617 2-Oct-13 7:34am    
I boot the names into 2 combobox, and the user need to select two files from the comboboxes. and there is a program that compares them. so i can not give the user the option to select the same file twice...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900