How to populate a Combo Box in C # using file names from Dir?

Hope you can help me with this problem.

I am trying to populate a combo box with the names of a specific directory. This DIR will always be the same, so it will always be the same.

Any ideas?

Hooray!

+4
source share
4 answers

When you initialize, do the following:

private void Form1_Load(object sender, EventArgs e) { string[] files = System.IO.Directory.GetFiles(@"C:\Testing"); this.comboBox1.Items.AddRange(files); } 
+5
source
 string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.txt"); foreach (string file in filePaths) { mycombobox.items.add(file); } 
+9
source

Or if you use WPF

 <Grid> <ComboBox x:Name="DirectoriesComboBox" Width="100" Height="25"></ComboBox> </Grid> string [] array = Directory.GetFiles(@"C:\Test"); DirectoriesComboBox.ItemsSource = array; 
+2
source

You can do this by adding a link to system.IO and using this code: (DDLFolder is a drop-down list, and if you are writing an ASP.Net application to get the path, use Server.Mappath ("~ / yourpath"))

 DirectoryInfo df = new DirectoryInfo(userFolderPath); DDLFolder.Items.Clear(); DDLFolder.Items.Add("Root"); foreach (DirectoryInfo d in df.GetDirectories()) { DDLFolder.Items.Add(d.Name); } 
0
source

Source: https://habr.com/ru/post/1341319/


All Articles