.NET Directory.Exists 在以管理员身份运行时否认映射网络驱动器的存在

发布于 2024-11-26 02:15:50 字数 852 浏览 1 评论 0原文

我正在 Windows 7 上编写一个小型 .NET 程序。它需要做的一件事是创建符号链接,这似乎需要我具有管理员权限。它还需要能够使用映射的网络驱动器(例如,R:\,在我的系统上映射到 \\titanium\Private\)。

我正在使用 Directory.Exists(path) 来验证路径是否存在。

当以普通用户(管理员帐户,但不是“以管理员身份”)运行程序时,这在映射的网络驱动器上运行良好。

当以管理员身份(使用 UAC)运行该程序时,它无法找到存在的目录。结果,程序拒绝承认R:\Steam Games\是一个实际存在的目录。

我有点困惑为什么会发生这种情况。使用完整的 UNC 路径 (\\titanium\Private\Steam Games\) 也不起作用。

以前有人遇到过这个吗?有什么好的解决办法吗?我是否必须将路径格式化为不同的格式(注意:大多数路径目前都使用 Path.Combine 进行格式化,因此它们应该是正确的)。

感谢您的帮助。

(例如,Directory.Exists(@"R:\Steam Games\") 在以管理员身份运行时返回 false,但该文件夹存在。定期运行时,函数调用会正确返回 true)。

编辑:问题确实似乎是管理员在技术上是不同的用户帐户。我什至无法使用 UNC 路径,因为我仅在常规用户下登录到文件服务器,而不是在“管理员”下登录。作为一种(相对黑客的)解决方法,我只是使用常规权限运行我的程序,然后使用 Process.Start 来调用 cmd.exe 的实例,并使用参数创建符号链接(以及动词“runas”) ”以获取 UAC 提示)。

I'm writing a small .NET program on Windows 7. One thing it needs to do is to create symbolic links, which seems to require me to have administrator privileges. It also needs to be able to work with mapped network drives (for example, R:\, which on my system maps to \\titanium\Private\).

I'm using Directory.Exists(path) to verify that a path exists.

When running the program as a regular user (administrator account, but not "as administrator"), this works fine on the mapped network drive.

When running the program as an administrator (with UAC), it fails to find directories that exist. As a result, the program refuses to acknowledge that R:\Steam Games\ is a directory that actually exists.

I'm a bit baffled as to why this is happening. Using the full UNC path (\\titanium\Private\Steam Games\) also doesn't work.

Has anyone run into this before? Is there any good workaround? Do I have to format the paths different (note: most of them are currently formatted with Path.Combine, so they should be correct).

Thanks for your help.

(As an example, Directory.Exists(@"R:\Steam Games\") returns false when running as an admin, but that folder exists. The function call correctly returns true when running regularly).

Edit: The issue indeed appears to be that an administrator is technically a different user account. I could not even use UNC paths, because I was only logged in to my fileserver under my regular user, not under "Administrator". As a (relatively hackish) workaround, I just run my program with regular privileges and then use Process.Start to invoke an instance of cmd.exe with the arguments to create a symbolic link (and verb "runas" to get the UAC prompt).

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

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

发布评论

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

评论(3

空名 2024-12-03 02:15:50

Windows 中的映射驱动器与用户上下文相关联。映射到帐户:UserA 的驱动器将无法被 UserB 访问。不过,您可以在不同用户下创建映射到相同驱动器号的相同 unc 路径。

使用完整的 unc 路径应该可以。

更多信息请访问 support.microsoft.com:12

Mapped drives in windows are tied to a user context. A drive mapped to a account: UserA, will not be accessible to UserB. You could create the same unc path mapped to the same drive letter under different users though.

Using the full unc path should work though.

More info at support.microsoft.com: 1, 2.

樱花落人离去 2024-12-03 02:15:50

不起作用的根本原因是IIS用户没有访问该路径的权限。当以普通用户身份运行时,访问权限将授予用户,即要使其正常工作,您需要授予 IIS 文件夹的权限
请按照以下方式在 iis 运行的服务中使用 UNC 路径:

属性 -->安全性-->编辑-->添加--> IIS_WPG

The basic reason why it does not work is that IIS user does not have permission to access the path. When running as regular user the access is given to the user i.e to make it work you need to give permission to the folder for IIS
Follow as below for UNC path to work in services run by iis:

Properties --> Security --> Edit--> Add --> IIS_WPG

浮世清欢 2024-12-03 02:15:50

此处对此进行了讨论。显然,在 .Net Framework 的早期版本 文档明确提到这是不可能的(“Exists 方法不执行网络身份验证。如果您在未预先身份验证的情况下查询现有网络共享,Exists 方法将返回 false。” )。 4.0 文档不再包含这句话,但行为没有改变。

我在此处找到了解决方法(但决定不使用它)。

This has been discussed here. Apparently, in earlier versions of the .Net Framework the documentation explicitly mentioned that this is not possible ("The Exists method does not perform network authentication. If you query an existing network share without being pre-authenticated, the Exists method will return false."). The 4.0 documentation no longer contains this sentence, but the behavior has not changed.

I found a workaround here (but decided against using it).

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