我正在尝试遍历整个目录树并将所有文件名打印在列表框控件上。我写了一些代码,但出现了错误。不确定我做错了什么。顺便说一句,这是在使用Visual Studio中的WPF的C#中。
这是Visual Studio中整个项目解决方案:http://tinyurl.com/a2r5jv9
如果您不想下载项目解决方案,则可在MainWindow.xaml.cs中找到代码:http://pastebin.com/cWRTeq3N
我也会在这里粘贴代码。
public partial class MainWindow : Window
{
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string sourcePath = @"C:\temp\";
static void DirSearch(string sourcePath)
{
try
{
foreach (string d in Directory.GetDirectories(sourcePath))
{
foreach (string f in Directory.GetFiles(d))
{
listBox1.Items.Add(f);
}
DirSearch(d);
}
}
catch (Exception ex)
{
listBox1.Items.Add(ex.Message);
}
}
}
}