解析Windows设备的驱动器号路径 [英] Resolve Windows device path to drive letter
问题描述
如何解析NT样式的设备路径,例如\Device\CdRom0
,为其逻辑驱动器号,例如G:\
吗?
How do you resolve an NT style device path, e.g. \Device\CdRom0
, to its logical drive letter, e.g. G:\
?
卷名与设备路径不同,因此不幸的是GetVolumePathNamesForVolumeName()
无法使用.
A Volume Name isn't the same as a Device Path so unfortunately GetVolumePathNamesForVolumeName()
won't work.
推荐答案
希望以下代码可以为您提供足够的解决方案-初始化后,您只需遍历集合即可找到您的匹配项.您可能需要先将所有内容转换为大写/小写,然后再插入集合中以帮助提高查询性能.
Hopefully the following piece of code will give you enough to solve this - after you've initialised it, you just need to iterate through the collection to find your match. You may want to convert everything to upper/lower case before you insert into the collection to help with lookup performance.
typedef basic_string<TCHAR> tstring;
typedef map<tstring, tstring> HardDiskCollection;
void Initialise( HardDiskCollection &_hardDiskCollection )
{
TCHAR tszLinkName[MAX_PATH] = { 0 };
TCHAR tszDevName[MAX_PATH] = { 0 };
TCHAR tcDrive = 0;
_tcscpy_s( tszLinkName, MAX_PATH, _T("a:") );
for ( tcDrive = _T('a'); tcDrive < _T('z'); ++tcDrive )
{
tszLinkName[0] = tcDrive;
if ( QueryDosDevice( tszLinkName, tszDevName, MAX_PATH ) )
{
_hardDiskCollection.insert( pair<tstring, tstring>( tszLinkName, tszDevName ) );
}
}
}
这篇关于解析Windows设备的驱动器号路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!