delphi - 为什么我的程序无法识别映射的 UNC 路径?

标签 delphi windows-7 delphi-2007 unc network-drive

我在从 Delphi 程序写入 Windows 7 中的映射网络驱动器 (P:) 时遇到一些问题。例如,当我尝试 ForceDirectories('P:\test\folder') 时,出现错误(找不到路径)。

我尝试在代码中输入 UNC 路径 (ForceDirectories('\\computername\share\test\folder')),效果很好。但是,ExpandUNCFileName('P:\') 似乎不起作用;它返回“P:\”。 在 Windows XP 上,ExpandUNCFileName('P:\') 返回 UNC 路径。

如何在 Windows 7 上的 Delphi 中获取 UNC 路径,或以其他方式写入映射的网络驱动器?

经过进一步调查,我好像缺少 Windows 中的某种初始化。我有另一个使用 TcxShellComboBox (DevExpress 组件)的应用程序 (app2)。导航到该组合框中的 P: 后,在 app2 中,对 ExpandUNCFileName 的调用在第一个应用程序中可以正常工作。与 P: 下的文件上的 FileExists 相同,在导航到 app2 中的 P: 之前返回 False,在计算机重新启动后和直到计算机重新启动之前返回 true。

最佳答案

根据Microsoft KB Article ,如果启用了用户帐户控制,并且从 Windows 资源管理器(非提升)映射网络驱动器,则提升的程序将无法访问该驱动器。引用:

If a user is logged on to Windows Vista or to Windows 7, and if User Account Control is enabled, a program that uses the user’s filtered access token and a program that uses the user’s full administrator access token can run at the same time. Because LSA created the access tokens during two separate logon sessions, the access tokens contain separate logon IDs.

When network shares are mapped, they are linked to the current logon session for the current process access token. This means that, if a user uses the command prompt (Cmd.exe) together with the filtered access token to map a network share, the network share is not mapped for processes that run with the full administrator access token.

由于您在评论中提到您“以管理员身份”(提升)运行 Delphi,所以这是您的问题。

解决方案:

  1. 如果不需要,请不要运行 Delphi 提升版本。如果您确实需要在软件中进行提升,请将其分为两部分(提升的和非提升的),并从第一部分访问映射的网络驱动器。然后使用提升的 COM 对象或简单地通过执行单独的可执行文件来访问另一部分。

  2. 从提升的网络提示符映射网络驱动器,以便提升的用户可以使用映射的网络驱动器:

    a.打开提升的命令提示符(以管理员身份运行“cmd.exe”)

    b.输入net use p:\\computername\share\test\folder

关于delphi - 为什么我的程序无法识别映射的 UNC 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7673958/

相关文章:

Delphi 2010 及早期编译器

windows - 部署 Datasnap 服务

audio - 为什么在Windows 7 PC上无法使用vb脚本使声音静音?

delphi - 即使文件存在,FileExists() 返回 false

delphi - 关于创建类结构的想法

delphi - 如何从服务应用程序使用 FindWindow()?

delphi - 如何在 TPaintBox 下方的 TGraphicControl 上使用 FindVCLWindow?

c# - 在 Windows 7 中安装 Windows 服务

delphi - 来自编译器的虚假 "Undeclared Identifier"错误?