Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

c++ - How to rename or relabel a Network Drive label

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 FolderName ,

NDSTestFolder on 'NAS server (172.24.17.116)'(R:)

I need to rename the drive label using SetVolumeLabel but this fails for the network drive saying invalid parameter while SetVolumeLabel works fine for the local drives.

Is there a way I can rename or relabel a network drive? So that I can change

NDSTestFolder on 'NAS server (172.24.17.116)'(R:)

to

NDS@MyFolder (R:)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...