我试图让 wpf 应用程序显示带有我的文件的树视图。我无法将 driveinfo 正确放入目录信息中:
DirectoryInfo sdf = new DirectoryInfo(DriveInfo.GetDrives()[x].ToString());
foreach (DirectoryInfo z in sdf.GetDirectories()) //Run time error here
{ }
它给出了运行时错误。以下是完整代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
treeView1.Items.Clear();
for (int x = 0; x < DriveInfo.GetDrives().Length; x++)
{
TreeViewItem y = new TreeViewItem() { Header = DriveInfo.GetDrives()[x] };
treeView1.Items.Add(y);
comboBox1.Items.Add(DriveInfo.GetDrives()[x]);
DirectoryInfo sdf = new DirectoryInfo(DriveInfo.GetDrives()[x].ToString());
foreach (DirectoryInfo z in sdf.GetDirectories()) //Run time error here
{
TreeViewItem newmain = new TreeViewItem();
newmain.Header = z.Name;
newmain.Tag = z.FullName;
y.Items.Add(newmain);
}
}
错误是:
IOException 未处理