2

I enumerated directories into a ListBox using this:

private void TE_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {            
IEnumerable<string> file = System.IO.Directory.EnumerateDirectories(@"C:\Users\user\Desktop", "*.*", System.IO.SearchOption.AllDirectories);
        foreach (var f in file)
        {
            lbz.Items.Add(String.Format(f));
        }
    } 

Now, the ListBox displays all the directories in that given path, then I use:

private void lbz_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (lbz.SelectedItem != null)
        {
            if (Directory.Exists(lbz.SelectedItem.ToString()))
            {
                string[] filePaths = Directory.EnumerateFiles() //:<------
                for (int i = 0; i < filePaths.Length; ++i) 
                {
                    lbz2.Items.Add(i); 
                }

            }
            else
            {
                tb1.Text = "Directory Doesn't Exist On This Path";
            }
        }
        else
        {
            tb1.Text = "No Directory Selected";
        }

    }

The arrow is where I'm stumped, since I'm using Microsoft Visual Web Developer I can't use GetFiles, I have to use Enumerate.

I want to be able to populate another ListBox (lbz2) by selecting a directory in lbz and having that directories contents, all the files in it, displayed in lbz2.

If:

string[] filePaths = Directory.EnumerateFiles() //:<------
for (int i = 0; i < filePaths.Length; ++i) 
{
    lbz2.Items.Add(i); 
}

doesn't work, I'm open to suggestions.

pb2q
  • 58,613
  • 19
  • 146
  • 147
FamenZ
  • 41
  • 6

1 Answers1

2

This should work:

foreach (var filePath in Directory.EnumerateFiles(lbz.SelectedItem.ToString()))
{
    lbz2.Items.Add(filePath); 
}

EnumerateFiles returns IEnumerable<string>, not string[].

FamenZ
  • 41
  • 6
Tomek
  • 785
  • 4
  • 10
  • It works, I had to remove "string[] filePaths = Directory.EnumerateFiles() //:<------ for (int i = 0; i < filePaths.Length; ++i)" and add the "foreach" it works wonderfully, thank you. =) – FamenZ Jul 12 '12 at 15:57