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.

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.