Search code examples
c#loopssubdirectorygetdirectories

Listing Folders in a List box with specific Subfolder - C#


I have a combobox with an array of Language codes - DA, DE , ET etc.

I have a directory with 11 folders listed from "A" to "K" and each of these folders contain 5 subfolders named after 5 random language codes

I want to be able to select a language in the combobox and click my search button and it will list me only the folders that have that specific language code folder in it

I want it to display the folder names in a listbox.

This is not a homework question - I am slowly learning C# and messing with different instances so i can understand them.

I have tried everything and cant get it to display.

This is what i have done thus far:

It is rather messy but Its all trial and error for me hence the stupid amount of comments

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

I Have tried the above and some of the suggested below But i keep getting this error (Click above). I want it to search through the directories and list the folders which have subfolders named according to the ComboBox.SelectedItem()


Solution

  • The problem is in your foreach loop, You set your listbox's data source to your directory name.

    If you want to set the datasource of your listbox, you should use a list. In your case you can add strings as items Like this :

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

    In this case you should pay attention to clearing your listbox because if you use this code :

    listBox4.DataSource = myList;
    

    before filling the listbox, it will clear automaticly. But if you call

     listBox.Items.Add(dirname); 
    

    It will dupplicate items if you call your methode more than one times.