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.
This should work:
foreach (var filePath in Directory.EnumerateFiles(lbz.SelectedItem.ToString()))
{
lbz2.Items.Add(filePath);
}
EnumerateFiles returns IEnumerable<string>
, not string[]
.