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 am getting error when I make selection from comboBox 1 as direcory doesnot exists at bin/debug path of the project.

  private void Form1_Load(object sender, EventArgs e)
        {
 DirectoryInfo di = new DirectoryInfo(@"\\Path\CAMR");
                var tempDirectoryCollection = di.GetDirectories();
                int pathCount = tempDirectoryCollection.Count();
                var paths = new String[pathCount];
                int i = 0;
                for (; i < pathCount; i++)
                {
                    var fi = tempDirectoryCollection[i];
                    comboBox2.Items.Add(fi.Name);
                    paths[i] = fi.FullName;
                }
                comboBox2.SelectedIndex = 0;
                }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
             comboBox2.Items.Clear();
             //string dinf01 = comboBox2.SelectedItem.ToString();

             DirectoryInfo dir = new DirectoryInfo(comboBox2.SelectedItem.ToString());
             FileInfo[] files = dir.GetFiles("*.tif");
             int i = 0;
             //DirectoryInfo dir = (DirectoryInfo)comboBox2.SelectedItem;

             foreach (FileInfo fi in files)
             {
                 comboBox2.Items.Add(fi.Name);
             }
             foreach (FileInfo finf in files)
             {
                 paths[i] = finf.FullName;
                 i++;
             }

Any help and suggestions appreciated.

Dani AI

Generated

Short diagnosis and a cleaner pattern to avoid the bin\Debug path error

The symptom in the thread is caused by using a name-only string (or the wrong ComboBox) when constructing a DirectoryInfo — a name-only value is treated as a relative path, which resolves to the process working directory (bin\Debug) and leads to "directory does not exist". In the original posts added the directory Name into the UI and later tried to build a DirectoryInfo from the displayed text (and the selection code referenced the wrong control). That mismatch is the root cause.

A more robust approach is to keep the display name and the full path together (bind an object or use ValueMember), then always use the full path for file/dir operations. Example pattern:

var root = @"\\Path\CAMR";
var dirs = Directory.EnumerateDirectories(root)
    .Select(p => new { Name = Path.GetFileName(p), Full = p })
    .ToList();

comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Full";
comboBox1.DataSource = dirs;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var full = comboBox1.SelectedValue as string;
    if (string.IsNullOrEmpty(full) || !Directory.Exists(full)) return;
    var files = Directory.EnumerateFiles(full, "*.tif").Select(Path.GetFileName).ToList();
    comboBox2.DataSource = files;
}

Practical checks and tips

  • Never call SelectedItem.ToString() without checking SelectedIndex/-Value first; after clearing items SelectedItem can be null.
  • Always verify Directory.Exists(full) and catch IOException/UnauthorizedAccessException for network shares.
  • Use EnumerateFiles/EnumerateDirectories for large folders (lazy enumeration).
  • If not binding, store full paths in the ComboBox items (or in a parallel list/Tag) instead of relying on display text.

Remarks on the thread
@tinstaafl’s suggestion to construct full paths fixed the immediate problem; the binding/value-member pattern above reduces fragile string handling and scales better for maintenance.

Solution is to use path.combine as suggested by tinstaafl.

 private void Form1_Load(object sender, EventArgs e)
        {
 DirectoryInfo di = new DirectoryInfo(@"\\Path\CAMR");
                var tempDirectoryCollection = di.GetDirectories();
                int pathCount = tempDirectoryCollection.Count();
                var paths = new String[pathCount];
                int i = 0;
                for (; i < pathCount; i++)
                {
                    var fi = tempDirectoryCollection[i];
                    comboBox2.Items.Add(fi.Name);
                    paths[i] = fi.FullName;
                }
                comboBox2.SelectedIndex = 0;
                }
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
             comboBox2.Items.Clear();
             string path1 = @"\\Path\CAMR";
             string path2 = comboBox2.SelectedItem.ToString();
             string fullpath = Path.Combine(path1, path2);
             DirectoryInfo dir = new DirectoryInfo(fullpath);
             var tempfilecollection = dir.GetFiles("*.tif");
         int pathcount = tempfilecollection.Count();
         paths = new string[pathcount];
         int i = 0;
         for (; i < pathcount; i++)
         {
             var fi = tempfilecollection[i];
             comboBox3.Items.Add(fi.Name);
             paths[i] = fi.FullName;
         }
         }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.