因此,我关注了MS文章http://msdn.microsoft.com/en-us/library/ms171645.aspx

这是使用设计器使用ListView和TreeView控件创建一个Explorer风格的接口。

因此,一切都很好,但是,是否将其设置为C的根目录以扫描所有文件夹和文件等。我收到{"Access to the path '<path to file' is denied."}

VS 2010指出了这个问题所在。

subSubDirs = subDir.GetDirectories();

但是,我可以尝试解决这个问题,但是在引发异常之后,该应用程序将无法继续运行。

有没有一种方法可以跳过应用程序无法访问的目录?

最佳答案

您可能将try catch放在错误的位置。根据演练中的代码,您可以像这样放置try catch:

更换:

subSubDirs = subDir.GetDirectories();


有了这个:

try
{
    subSubDirs = subDir.GetDirectories();
}
catch(UnauthorizedAccessException uae)
{
  //log that subDir.GetDirectories was not possible
}


另外,该行:

if (subSubDirs.Length != 0)


应该更改为:

if (subSubDirs != null && subSubDirs.Length != 0)

关于c# - C#treeview未经授权的异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9070859/

10-13 06:38