How to fill a Combo Box on C# with the namefiles from a Dir?
I hope you can help me with this problem.
I've been trying to开发者_StackOverflow fill a combobox with the namefiles of a specific directory. This DIR will be always the same so it will be the same routine always.
Any ideas?
Cheers!
string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.txt");
foreach (string file in filePaths)
{
mycombobox.items.add(file);
}
When you initialize do this:
private void Form1_Load(object sender, EventArgs e)
{
string[] files = System.IO.Directory.GetFiles(@"C:\Testing");
this.comboBox1.Items.AddRange(files);
}
Or if you are using WPF
<Grid>
<ComboBox x:Name="DirectoriesComboBox" Width="100" Height="25"></ComboBox>
</Grid>
string [] array = Directory.GetFiles(@"C:\Test");
DirectoriesComboBox.ItemsSource = array;
You can do so by adding a reference to system.IO and using this code: (DDLFolder is you dropdown list, and if you are writing an ASP.Net application for getting 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);
}
精彩评论