C# 验证远程目录是否存在

发布于 2024-11-03 01:40:09 字数 261 浏览 2 评论 0原文

我正在使用 Directory.Exists 来查看是否输入的目录是有效目录。这对于所有本地目录以及我有权访问的有效远程目录都适用,但对于我无权访问的远程目录,失效速度很慢。

我怀疑这是因为内置的尝试查找该目录的次数都失败了,因为我无权访问它。

如何更快地确定远程目录无效?

I am using Directory.Exists to see if an entered directory is a valid directory. This works well for all local directories, and for valid remote directories that I have access to, but for remote directories that I don't have access to, it is slow to invalidate.

I suspect that this is because of a built in set number of attempts to find the directory, which are all failing because I don't have access to it.

How can I determine a remote directory is invalid faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墟烟 2024-11-10 01:40:09

对于稍微不同的方法,可能值得尝试 DirectoryInfo,在内部它可能使用与 Directory.Exists() 不同的方法:

DirectoryInfo di = new DirectoryInfo(yourPath);
if(di.Exists())
{
    ...
}

但要注意, 构造函数 在某些情况下似乎会抛出异常。
由于这不是静态类,因此它的性能可能更差,但我认为值得尝试。

For a slightly different approach, it may be worth giving DirectoryInfo a try, internally it may use a different approach than Directory.Exists():

DirectoryInfo di = new DirectoryInfo(yourPath);
if(di.Exists())
{
    ...
}

But watch out, the constructor seems to throw exceptions in some cases.
Since this is not a static class, it may perform even worse, but it's worth trying I'd think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文