如何从映射驱动器的ExpandUNCFileName函数返回路径?

时间:2017-04-24 17:06:17

标签: delphi windows-10

我正在使用Delphi 5.我最初在从网络映射驱动器复制文件时遇到问题。如果我使用UNC路径,我可以复制该文件。问题是我不会总是知道UNC路径。我使用的是Windows 10,64位。有没有一个解决方案可以让我获得UNC路径?每次我使用ExpandUNCFileName函数时,它返回路径N:\ tmp \ file1.mdb而不是\ hermes \ apps \ tmp \ file1.mdb。如果我在XP或Windows 7上运行代码,它会正确返回UNC路径,但是当我在Windows 10上运行它时,它会失败并给我带有驱动器号的路径。

我要做的是运行一个更新数据库文件的安装程序。要使它工作,我需要在本地复制数据库文件。现在问题出现在我第一次启动更新程序exe时,我的Windows 10强制程序运行管理员。一旦我运行exe,它就会启动管理员登录。它只在Windows 10上执行此操作。对于Windows 7,XP它不会要求或强制它以管理员身份运行。

如果可以,请帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

ExpandUNCFileName works correctly. If you call that function from a context which has the drive letter mapped, then it will return the desired UNC name.

The problem therefore must be that when you call the function the drive is not mapped. Common reason for that would be that your code was executing in a service, or under a user account that does not map the drive, but there are other reasons.

Exactly how to solve your problem is not clear from here. You might solve it by making sure that the drive letter is always mapped. However, this is a potentially brittle approach, as you are discovering.

Perhaps a more reliable approach would be to operate exclusively with UNC names. This after all is the reason for their existence. So, when the user provides you a path to a network directory or file, convert it at that point to a UNC name. From that point on use that UNC name.

If you don't want your program to be elevated then you need to either give it an application manifest, or change its name. Executables without application manifests, and with names like update or patch, are elevated by UAC aware versions of Windows. UAC was introduced 12 years ago, it is perhaps time for you to get up to speed with the implications for developers. Not least because your old Delphi 5 development environment predates UAC, and therefore will not generate and link a manifest for you.