我正在使用WNetAddConnection2将网络驱动器安装到Windows,这可以正常工作但默认情况下安装驱动器时会将名称指定为服务器IP和FolderName,
'NAS服务器上的NDSTestFolder(172.24.17.116)'(R:)
我需要使用SetVolumeLabel重命名驱动器标签,但是对于网络驱动器说无效参数会失败,而 SetVolumeLabel 适用于本地驱动器。
有没有办法重命名或重新标记网络驱动器?这样我就可以改变了
'NAS服务器上的NDSTestFolder(172.24.17.116)'(R:)
到
NDS @ MyFolder(R:)
答案 0 :(得分:3)
将您的脚本翻译成C ++:
#include <shldisp.h>
void RenameDrive(BSTR strNewName, BSTR strDriveLetter)
{
IShellDispatch* pShellDispatch = NULL;
HRESULT hr = CoCreateInstance(CLSID_Shell,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellDispatch,
(void**)&pShellDispatch);
if (SUCCEEDED(hr) && pShellDispatch)
{
Folder* pFolder = NULL;
VARIANT vt = {};
VariantInit(&vt);
vt.vt = VT_BSTR;
vt.bstrVal = strDriveLetter;
hr = pShellDispatch->NameSpace(vt, &pFolder);
VariantClear(&vt);
if (SUCCEEDED(hr) && pFolder)
{
Folder2* pFolder2 = NULL;
hr = pFolder->QueryInterface(IID_Folder2, (void**)&pFolder2);
if (SUCCEEDED(hr) && pFolder2)
{
FolderItem* pFolderItem = NULL;
hr = pFolder2->get_Self(&pFolderItem);
if (SUCCEEDED(hr) && pFolderItem)
{
pFolderItem->put_Name(strNewName);
pFolderItem->Release();
}
pFolder2->Release();
}
pFolder->Release();
}
pShellDispatch->Release();
}
}
答案 1 :(得分:1)
不确定是否有API,但字符串存储在注册表中:
HKEY_CURRENT_USER \软件\微软\的Windows \ CurrentVersion \ Explorer中\ MountPoints2
通过解析那里的键找到正确的子键(它可能看起来像“## 172.24.17.116#NDSTestFolder”)并将_LabelFromReg值更改为你喜欢的任何值......
答案 2 :(得分:0)
我尝试使用vb脚本重命名驱动器:
LPCTSTR szCode = "Function RenameDrive(strNewName,strDriveLetter) \r\n\
Dim objShell \r\n\
Set objShell = CreateObject(\"Shell.Application\") \r\n\
'if objShell is Not Nothing Then \r\n\
objShell.NameSpace(strDriveLetter).Self.Name = strNewName \r\n\
'End if \r\n\
End Function";