How do I programmatically change the label of a mapped drive?

后端 未结 2 2150
囚心锁ツ
囚心锁ツ 2021-02-19 16:45

I\'m writing a piece of software which maps a network drive using the WNetAddConnection2 API. Just in case it\'s relevant, this is a WebDAV drive, rather than a normal SMB shar

相关标签:
2条回答
  • 2021-02-19 16:59

    You could use PowerShell in your C# code, the https://msdn.microsoft.com/en-us/library/system.management.automation.powershell(v=vs.85).aspx

    Change DriveLetter E to Q with PowerShell

    $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'e:'"
    Set-WmiInstance -input $drive -Arguments @{DriveLetter="Q:"; Label="Label"}
    
    0 讨论(0)
  • 2021-02-19 17:05

    I know the question is old already but I had exactly the same issue with the renaming of a webdav drive letters and found a solution.

    The problem occurs, if you ever connected your webDav drive with the address like: https://www.myurl.com:5006/myFolder

    Using this url-scheme will result in an registry entry in:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2
    A "DavWWWRoot" will automatically inserted into your given address: ##myurl.com@SSL@5006#DavWWWRoot#myFolder
    Also if you remove this drive, the RegistryEntry persists and will prevent you from renaming the drive via script also if you already made it to mount a drive with a registry-entry without the "DavWWWRoot" string in it. (using different url-scheme)

    When you try to automatically rename the drives label with the method showed by Dan, the new name will be placed unter a new RegistryEntry with a different path (without the "DavWWWRoot") and the new name will not be used.

    The solution is:

    1. Unmount your webDav-drives you want to rename.
    2. Delete the RegistryEntries with the DAVWWWRoot in it for all your drives you want to rename from `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2´
    3. Mount your webDav-drives with a slightly adapted url-scheme. For our example use a url-scheme like this one:
      \\www.myurl.com@SSL@5006\myFolder
    4. Check if a registry-entry was added without "DAVWWWRoot" in it. Should be:
      ##myurl.com@SSL@5006#myFolder
    5. Now you can go and rename your drive-label with the method of Dan:
      Shell32.Shell shell = new Shell32.Shell(); ((Shell32.Folder2) shell.NameSpace("Z:")).Self.Name = "Shell";
    0 讨论(0)
提交回复
热议问题