【问题标题】:Listing Folders in a List box with specific Subfolder - C#在具有特定子文件夹的列表框中列出文件夹 - C#
【发布时间】:2017-12-06 11:17:18
【问题描述】:

我有一个包含语言代码数组的组合框 - DA、DE、ET 等。

我有一个目录,其中包含从“A”到“K”列出的 11 个文件夹,每个文件夹都包含 5 个以 5 个随机语言代码命名的子文件夹

我希望能够在组合框中选择一种语言并单击我的搜索按钮,它只会列出包含该特定语言代码文件夹的文件夹

我希望它在列表框中显示文件夹名称。

这不是一个家庭作业问题 - 我正在慢慢学习 C# 并处理不同的实例以便我能理解它们。

我已经尝试了所有方法,但无法显示。

这是我迄今为止所做的:

它相当混乱,但对我来说都是反复试验,因此愚蠢的 cmets 数量

string Download = @"\\Mgsops\data\B&C_Poker_Builds\Release_Location\Tools\Language_Detection\DownloadBrands";

string DLLangs = @"\\Mgsops\data\B&C_Poker_Builds\Release_Location\Tools\Language_Detection\DownloadTrunk";


private int Total_Plus_EN = 0;

public Form1()
{
    InitializeComponent();


    DirectoryInfo languages = new DirectoryInfo(DLLangs);
    DirectoryInfo[] Dir = languages.GetDirectories();
    //languageBox.DataSource = Dir;
   //languageBox.SelectedIndex = 1;
   // languageBox.Items.RemoveAt(0);
    languageBox.Items.AddRange(Dir);
    languageBox.Items.RemoveAt(0);
    languageBox.SelectedIndex = 0;

    int Number = Dir.Length;
    //plus EN
    int Total_Plus_EN = 1 + Number;
    //Minus .SVN
    Total_Plus_EN = Total_Plus_EN - 1;
    string myString = Total_Plus_EN.ToString();
    textBox1.Text = myString;


}



private void button1_Click(object sender, EventArgs e)
{
    //Tried and tested 

    //IEnumerable<string> list = Directory.GetDirectories(Download).Where(s => s.Equals(languageBox.SelectedItem));
    //listBox4.DataSource = list.ToString();
    //for (int langIndex = 1; langIndex <= Total_Plus_EN; langIndex++)
    // {
    // }
    //string[] filesArray = Directory.GetFiles(Download);
    //listBox4.Items.AddRange(filesArray);

    listBox4.Items.Clear();

    string Local_Folder = @"\local";
    string backslash_Value = @"\";


    DirectoryInfo clients = new DirectoryInfo(Download);
    DirectoryInfo[] ClientNames = clients.GetDirectories();

    //listBox.DataSource = folders;          
   //  listBox4.Items.AddRange(ClientNames);
   // listBox4.Items.RemoveAt(0);

    string Destination_to_Lang = Download + backslash_Value + ClientNames + Local_Folder + backslash_Value + languageBox.SelectedItem ;

  //  DirectoryInfo langfolders = new DirectoryInfo(Destination_to_Lang);
    //DirectoryInfo[] PrintLang = langfolders.GetDirectories(languageBox.SelectedItem, SearchOption.AllDirectories);

    foreach (var d in Directory.GetDirectories(Destination_to_Lang))
    {
        var dirname = d.Substring(d.LastIndexOf('\'') + 1);
        listBox4.DataSource = dirname;
    }            
}}

View Error message

我已经尝试了上面的方法和下面的一些建议但是我不断收到这个错误(点击上面)。 我希望它搜索目录并列出具有根据 ComboBox.SelectedItem() 命名的子文件夹的文件夹

【问题讨论】:

  • 那么到底是什么问题?你得到什么输出?这与你想要的有什么不同?
  • @PaulF - 无论我尝试什么都不显示 - 抱歉忘记添加问题
  • 最明显的问题是“listBox4.DataSource = dirname;”您对获取目录列表中的每一件事都执行此操作,而不是构建重要的目录列表并分配它

标签: c# loops subdirectory getdirectories


【解决方案1】:

问题出在你的 foreach 循环中, 您将列表框的数据源设置为您的目录名称。

如果要设置列表框的数据源,则应使用列表。 在您的情况下,您可以像这样添加字符串作为项目:

foreach (var d in Directory.GetDirectories(Destination_to_Lang))
{
    var dirname = d.Substring(d.LastIndexOf('\'') + 1);
    listBox4.Items.Add(dirname);
}            

在这种情况下,您应该注意清除列表框,因为如果您使用此代码:

listBox4.DataSource = myList;

在填充列表框之前,它会自动清除。但是如果你打电话给

 listBox.Items.Add(dirname); 

如果你多次调用你的方法,它会重复项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多