How to rename or relabel a Network Drive label

前端 未结 3 480
-上瘾入骨i
-上瘾入骨i 2020-12-20 07:20

I am mounting a network drive to windows using WNetAddConnection2 which is working fine but while mounting the drive by default it assigns the name as Server IP and FolderNa

相关标签:
3条回答
  • 2020-12-20 08:04

    I tried using vb script for renaming thr drive:

    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";
    
    0 讨论(0)
  • 2020-12-20 08:11

    Not sure if there's an API for this but the strings are stored in the registry under:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2

    Find the correct subkey by parsing the keys there (it will probably look like "##172.24.17.116#NDSTestFolder" ) and change the _LabelFromReg value to whatever you like...

    0 讨论(0)
  • 2020-12-20 08:14

    Translated your script into 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();
       }
    }
    
    0 讨论(0)
提交回复
热议问题