Click here to Skip to main content
15,922,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to upload folders to comboBox 1 from the directory, and then at selection from comboBox 1 to comboBox2.

I have below code, I ma getting error when I make selection from comboBox 1 as "Unbable to cast object of the type system.string to system.Io.DirectoryInfo

C#
private void Form1_Load(object sender, EventArgs e)
{

    DirectoryInfo di = new DirectoryInfo("C:/Project");
    foreach (DirectoryInfo dinf in di.GetDirectories())
    {
        comboBox1.Items.Add(dinf.Name);
    }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox2.Items.Clear();
    try
    {
        DirectoryInfo dinfo01 = (di)ComboBox1mboBox1.SelectedItemtem;
    
        foreach (FileInfo fi dinfo01.GetFiles())
            comboBox2.Item.Add(fi);
    
    }
    Catch (Exception ex)
    {
        MessageBox.Show(ex.message);
    }
    
}


Any help and suggestions appreciated?
Posted
Updated 13-Nov-15 4:55am
v3

From the code, I assume this is the line causing the exception:
DirectoryInfo dinfo01 = (di)ComboBox1mboBox1.SelectedItemtem;

The exception is clear. You are trying to convert SelectedItem, which is a string in your case into DirectoryInfo, which is not possible.

The SelectedItem will be a string, because you are binding dinf.Name to comboBox1
comboBox1.Items.Add(dinf.Name);

and you can convert it back to a string.

One solution will be to:
1) Get the directory name using SelectedItem
2) Use a foreach or LINQ to find the DirectoryInfo based on the name from Step 1
3) Find the file info of directory from step 2 and populate comboBox2.

Code may look something like this:
string dinfo01 = ComboBox1mboBox1.SelectedItemtem.ToString();
DirectoryInfo di = new DirectoryInfo("C:/Project");
foreach (DirectoryInfo dinf in di.GetDirectories())
{
  if(dinf.Name.Equals(dinfo01))
  {
      foreach (FileInfo fi dinf.GetDirectories())
      comboBox2.Item.Add(fi);
  }
}


Note: This is just a sample code to give you an idea and it is not tested.
Hope this helps :)
 
Share this answer
 
Comments
Member 12076824 13-Nov-15 10:52am    
Thanks for response, it gives error as " cannot convert type System.IO.DirectoryInfo to System.IO.FileInfo"

Any suggestions to amend this?
One way to approach this:
C#
// define a Dictionary to map Folder Names to DirectoryInfo(s)
private Dictionary<string,> NameToDirInfo = new Dictionary<string,>();

private void FillComboBoxes_Click(object sender, EventArgs e)
{
    DirectoryInfo di = new DirectoryInfo(@"C:\WhatEver\whatever");

    foreach (DirectoryInfo dinf in di.GetDirectories())
    {
        NameToDirInfo.Add(dinf.Name, di);
        comboBox1.Items.Add(dinf.Name);
    }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DirectoryInfo dinfo01 = NameToDirInfo[comboBox1.Text];

    foreach (FileInfo fi in dinfo01.GetFiles())
    {
        comboBox2.Items.Add(fi);
    }
}
Notes:

1. one (of many) reasons your code won't compile is because you try to use 'foreach on a collection of Directories mapped to 'FileInfo: so you need to use 'GetFiles if your intent is to get files, not directories.

2. be clear that the code above will only put in ComboBox2 the files found in the DirInfo selected in ComboBox1: it will ignore any directories in that DirInfo.

3. Of course, if you use only two ComboBoxes, you are limited to showing only two "levels:" if that's what you want, fine.

4. If you want to show a true hierarchy, you need use a TreeView.
 
Share this answer
 
Comments
Member 12076824 13-Nov-15 11:14am    
Thanks for response Bill.

I am sorry my bad, I posted wrong code. I have GetFiles in foreach on a collection of directories mapped to fileInfo.

Basically my aim is just to have files from folder in comboBox1 to be populated in comboBox2( which are .tiff images), and after that it will be displayed in pictureBox.

Code looks fine, but I have question about

private Dictionary<string,> NameToDirInfo = new Dictionary<string,>();
it gives error as type expected, can you please help me with this?

I appreciate all your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900