问题描述
我想知道是否存在使用驱动器号(例如X:\foo\bar.txt
)将路径解析为等效的UNC路径的通用方法,该路径可能是以下之一:
I wonder if there is a universal way of resolving a path using a drive letter (such as X:\foo\bar.txt
) into its equivalent UNC path, which might be one of the following:
-
X:\foo\bar.txt
,如果X:
是真实驱动器(即硬盘,USB记忆棒等) -
\\server\share\foo\bar.txt
(如果X:
是安装在\\server\share
上的网络驱动器) -
C:\xyz\foo\bar.txt
如果X:
是SUBST
命令将X:
映射到C:\xyz
的结果
X:\foo\bar.txt
ifX:
is a real drive (i.e. hard disk, USB stick, etc.)\\server\share\foo\bar.txt
ifX:
is a network drive mounted on\\server\share
C:\xyz\foo\bar.txt
ifX:
is the result of aSUBST
command mappingX:
toC:\xyz
我知道有部分解决方案,将:
I know that there are partial solutions which will:
-
解析网络驱动器(例如,参见(依赖于
WNetGetUniversalName
)
解析SUBST
驱动器盘符(请参阅QueryDosDevice
,该驱动器盘符按预期工作,但不会为诸如本地驱动器或网络驱动器之类的东西返回UNC路径).
Resolve the SUBST
drive letter (see QueryDosDevice
which works as expected, but does not return UNC paths for things such as local drives or network drives).
我是否缺少在Win32中实现此驱动器号解析的简单方法?还是我真的必须把WNetGetUniversalName
和QueryDosDevice
弄混才能得到我需要的东西?
Am I missing some straightforward way of implementing this drive letter resolution in Win32? Or do I really have to mess with both WNetGetUniversalName
and QueryDosDevice
to get what I need?
推荐答案
是的,您需要独立解析驱动器号.
Yes, you would need to resolve the drive letter independently.
WNetGetUniversalName()
接近,但仅适用于映射到实际UNC共享的驱动器号,但并非总是如此.没有一个API函数可以为您完成所有工作.
WNetGetUniversalName()
comes close, but only works for drive letters that are mapped to actual UNC shares, which is not always the case. There is no single API function that does all of the work for you.
这篇关于将Windows驱动器号解析为路径(替代和网络)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!