简体   繁体   English

如何使用Dir中的名称文件在C#上填充组合框?

[英]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 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. 这个DIR将始终相同,因此它将始终是相同的例程。

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 或者如果您使用的是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")) 您可以通过添加对system.IO的引用并使用以下代码来执行此操作:( DDLFolder是您的下拉列表,如果您正在编写ASP.Net应用程序以获取路径,请使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM