4

我列出了来自 Nas 共享的一些文件夹,获取文件夹属性并列出子文件夹。对于我没有访问权限的文件夹,我得到两个不同的例外

  1. System.UnauthorizedAccessException: 拒绝访问路径“Z:\info”。
  2. System.IO.IOException: 找不到网络路径。

第二个需要很长时间才能被捕获,这会减慢应用程序

我的问题是为什么对于某些共享我找不到网络路径而其他共享访问被拒绝?有什么不同?以及如何避免需要时间的第二个异常。

4

1 回答 1

4

我的问题是为什么对于某些共享我找不到网络路径而其他共享访问被拒绝?有什么不同?

如果快速找到路径,但您没有权限,您将很快收到UnauthorizedAccessException

但是,如果系统试图发现网络共享是否有效,这可能需要一段时间。在确定网络路径根本无效之前,它需要进行相当多的网络访问,这可能需要一些时间。

以及如何避免需要时间的第二个异常。

做到这一点的唯一真正方法是不访问不存在的共享。这并不总是可行的 - 如果您需要在运行时确定它们是否存在,您只需要围绕这可能很慢的事实进行设计。

There are many ways to improve this, however - if you're checking against multiple shares, you could do these checks in parallel. Given that this is really IO bound, threading could make a huge difference in overall responsiveness of your application as you can check all of the shares at the same time, instead of sequentially.

于 2013-03-29T15:09:08.527 回答