本文介绍了测试UNC路径的"前往的"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要访问一个特定的UNC路径,但该路径是跨域,所以根据机器的程序正在运行时,它可以或可以不具有默认凭证。通常情况下,用户将不得不只需打开浏览器并输入UNC或IP地址来获得登录提示(在这一点上,他们可以输入相应的凭据域的份额上)。

My program requires access to a certain UNC path, but the path is cross-domain, so depending on the machine the program is being run on, it may or may not have default credentials. Normally, a user would have to just open up explorer and type in the UNC or IP address to get a logon prompt (at which point they can type in appropriate credentials for the domain the share is on).

有一个干净的方式来测试UNC访问,如果默认的Windows凭据是坏的,然后促使他们对不同的人?

Is there a "clean" way to test the UNC accessibility, and if the default windows credentials are bad then prompt them for different ones?

现在,我使用一些讨厌的code尝试读取的共享文本文件,赶上一个IOException,然后打开的explorer.exe进程对象(隐藏),以获得登录提示。这是所有包含一个循环,10秒后再次检查范围内。它排序的作品,但解决的办法和逻辑似乎非常不可取的。

Right now I'm using some nasty code to try to read a text file on the share, catch an IOException, and then open an "explorer.exe" Process object (hidden) to get the logon prompt. This is all contained within a loop that checks again after 10s. It sort-of works, but the solution and logic seem really undesirable.

是我唯一的选择真的 WNetUseConnection 或互操作式的解决方案?

Are my only choices really WNetUseConnection or an interop-style solution?

推荐答案

检查这些文章:

  • Testing File Access Rights in .NET 2.0. A handy class for testing file system permissions.
  • Vexing exceptions. Eric Lippert on why pre-emptive testing is pointless (in a nutshell: there's an implicit race condition. Between the test and the actual access, some other process may change permissions, move or delete the file, or the network could drop...etc. This means you've got to deal with exceptions thrown when you try to access the resource, so you might well structure your code to deal with the problem when it actually occurs.

我用code是这样的:

I've used code like this:

new FileIOPermission(FileIOPermissionAccess.Read, path).Demand();

这是应该抛出一个 SecurityException异常,如果你不具备所需的访问,但在网络驱动器或UNC路径的路径中, FileIOPermission.Demand()的方法似乎是一个空操作。

which is supposed to throw a SecurityException if you don't have the desired access, but for paths on network drives or UNC paths, the FileIOPermission.Demand() method appears to be a no-op.

这篇关于测试UNC路径的"前往的"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:14