如何使用 System.IO.DirectoryInfo 访问映射的网络驱动器?
我需要在映射的网络驱动器上创建一个目录。 我正在使用代码:
DirectoryInfo targetDirectory = new DirectoryInfo(path);
if (targetDirectory != null)
{
targetDirectory.Create();
}
如果我指定像“\\\\ServerName\\Directory”这样的路径,一切都会正常。 如果我将“\\ServerName\Directory”映射为驱动器 Z:,并指定“Z:\\”等路径,则会失败。
创建 targetDirectory 对象后,VS 显示(在调试模式下)targetDirectory.Exists = false,并且尝试执行 targetDirectory.Create() 会引发异常:
System.IO.DirectoryNotFoundException: "Could not find a part of the path 'Z:\'."
但是,相同的代码可以很好地处理本地目录,例如 C:。
该应用程序是一个 Windows 服务(WinXP Pro、SP2、.NET 2),在与映射驱动器的用户相同的帐户下运行。 Qwinsta 回复说用户的会话是会话 0,因此它与服务的会话是同一个会话。
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.
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:
System.IO.DirectoryNotFoundException: "Could not find a part of the path 'Z:\'."
However, the same code works well with local directories, e.g. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我在 Win Server 2012 上遇到了同样的问题。禁用 UAC 解决了它。
I had the same problem on Win Server 2012. The disabling UAC solved it.
您可以尝试使用 WNetConnection 将映射的驱动器解析到网络小路。
You can try to use WNetConnection to resolve the mapped drive to a network path.
映射网络驱动器是特定于用户的,因此如果应用程序在与创建映射驱动器号 (z:) 的用户不同的身份下运行,它将无法工作。
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.
事实上,映射驱动器号不起作用,简单的解决方案是键入完整的网络路径。
也就是说,
我的
R:/
驱动器已映射到\\myserver\files\myapp\
因此,而不是使用
"R:/ " + "照片"
使用
"\\myserver\files\myapp\" + "照片"
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\
So instead of using
"R:/" + "photos"
use
"\\myserver\files\myapp\" + "photos"
您的应用程序运行所使用的帐户可能无权访问映射驱动器。 如果这是一个 Web 应用程序,那肯定是问题所在...默认情况下,Web 应用程序在 NETWORK SERVICE 帐户下运行,该帐户没有任何映射驱动器设置。 尝试使用模拟来查看是否可以解决问题。 尽管您可能需要找出更好的解决方案,然后仅使用模拟。 如果是我,我会坚持使用 UNC 路径。
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.
您是否使用与程序运行时完全相同的凭据进行映射?
Are you mapping with the exact same credentials as the program is running with?
您在 Vista/Server 2k8 上运行吗? 这两个服务都将服务隔离到会话 0 中,第一个交互式会话是会话 1。有更多信息 此处,关于会话隔离。 因此,即使同一用户用于服务和交互式登录,也将是不同的会话。
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.