C# 如何使用 System.IO.DirectoryInfo 访问映射的网络驱动器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/133660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access a mapped network drive with System.IO.DirectoryInfo?
提问by
I need to create a directory on a mapped network drive. I am using a code:
我需要在映射的网络驱动器上创建一个目录。我正在使用代码:
DirectoryInfo targetDirectory = new DirectoryInfo(path);
if (targetDirectory != null)
{
targetDirectory.Create();
}
If I specify the path like "\\\\ServerName\\Directory", it all goes OK. If I map the "\\ServerName\Directory" as, say drive Z:, and specify the path like "Z:\\", it fails.
如果我指定像“\\\\ServerName\\Directory”这样的路径,则一切正常。如果我将“\\ServerName\Directory”映射为驱动器 Z:,并指定像“Z:\\”这样的路径,则会失败。
After the creating the targetDirectory object, VS shows (in the debug mode) that targetDirectory.Exists = false, and trying to do targetDirectory.Create() throws an exception:
创建 targetDirectory 对象后,VS 显示(在调试模式下)targetDirectory.Exists = false,并尝试执行 targetDirectory.Create() 抛出异常:
System.IO.DirectoryNotFoundException: "Could not find a part of the path 'Z:\'."
However, the same code works well with local directories, e.g. C:.
但是,相同的代码适用于本地目录,例如 C:。
The application is a Windows service (WinXP Pro, SP2, .NET 2) running under the same account as the user that mapped the drive. Qwinsta replies that the user's session is the session 0, so it is the same session as the service's.
该应用程序是在与映射驱动器的用户相同的帐户下运行的 Windows 服务(WinXP Pro、SP2、.NET 2)。Qwinsta 回复用户的会话是会话 0,因此它与服务的会话相同。
回答by fhe
You can try to use WNetConnectionto resolve the mapped drive to a network path.
您可以尝试使用WNetConnection将映射驱动器解析为网络路径。
回答by EBGreen
Are you mapping with the exact same credentials as the program is running with?
您是否使用与程序运行时完全相同的凭据进行映射?
回答by Kev
Mapped network drives are user specific, so if the app is running under a different identity than the user that created the mapped drive letter (z:) it won't work.
映射的网络驱动器是特定于用户的,因此如果应用程序以与创建映射驱动器号 (z:) 的用户不同的身份运行,它将无法工作。
回答by Erikk Ross
The account your application is running under probably does not have access to the mapped drive. If this is a web application, that would definitely be the problem...By default a web app runs under the NETWORK SERVICE account which would not have any mapped drives setup. Try using impersonation to see if it fixes the problem. Although you probably need to figure out a better solution then just using impersonation. If it were me, I'd stick to using the UNC path.
您的应用程序在其下运行的帐户可能无法访问映射驱动器。如果这是一个 Web 应用程序,那肯定是问题所在……默认情况下,一个 Web 应用程序在 NETWORK SERVICE 帐户下运行,该帐户没有任何映射驱动器设置。尝试使用模拟来查看是否可以解决问题。尽管您可能需要找出更好的解决方案,然后仅使用模拟。如果是我,我会坚持使用 UNC 路径。
回答by Rob
Are you running on Vista/Server 2k8? Both of those isolate services into Session 0 and the first interactive session is Session 1. There's more info here, on session isolation. Thus, even if it's the same user being used for both the service and the interactive logon, it'll be different sessions.
你在 Vista/Server 2k8 上运行吗?这两项业务隔离到会话0,第一个交互式会话的会话是1,还有更多的信息在这里,在会话隔离。因此,即使服务和交互式登录使用的是同一个用户,也将是不同的会话。
回答by Doomsknight
Based on the fact, mapped drive letters don't work, the simple solution is to type the full network path.
基于事实,映射的驱动器号不起作用,简单的解决方案是键入完整的网络路径。
Aka,
阿卡,
my R:/
drive was mapped to \\myserver\files\myapp\
我的R:/
驱动器被映射到\\myserver\files\myapp\
So instead ofusing
所以而不是使用
"R:/" + "photos"
"R:/" + "photos"
use
用
"\\myserver\files\myapp\" + "photos"
"\\myserver\files\myapp\" + "photos"
回答by Orekhov Alexander
I had the same problem on Win Server 2012. The disabling UAC solved it.
我在 Win Server 2012 上遇到了同样的问题。禁用 UAC 解决了它。