Reputation: 2028
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 share.
The drive takes on a default name which I'd like to change.
Some answers on the net recommend using System.IO.DriveType, e.g:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (var drive in allDrives)
{
if (drive.DriveType == DriveType.Network && drive.Name.StartsWith("Z:"))
{
drive.VolumeLabel = "DriveInfo";
}
}
This unequivically does not work on network drives, and this backed up by MSDN, where it's stated that an UnauthorizedAccessException
Exception will be thrown.
Secondly, I attempted to use the shell method:
Shell32.Shell shell = new Shell32.Shell();
((Shell32.Folder2) shell.NameSpace("Z:")).Self.Name = "Shell";
The code executes with no errors, but the drive is not renamed. And this is where it gets weird, I found the registry path where these things get written:
HKEY_CURRENT_USER\ Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ MountPoints2
The code above creates a key which looks correct, and adds a _LabelFromReg
REG_SZ with "Shell" as the value. However, this is NOT reflected in Explorer or anywhere else.
I then manually renamed the mapped the drive, by right clicking and selecting "Rename".
Doing so creates a new, slightly different key within MountPoints2
which works perfectly.
So, the shell code above isn't quite resolving the path correctly - is there something else I can try? The above leads me to believe Windows must use a different API call internally to rename the drive?
Update 1
This is definitely related to these being WebDAV drives. There must be some under-the-hood processing going on.
My software maps https://myurl.com@ssl/stuff/destination/docs
. That exact path can be seen with the Net Use
command. It's this path that the shell
code above adds to the registry and attempts to name.
However, hovering over the drive in Windows Explorer gives me https://myurl.com@ssl/anotherfolder/stuff/destination
and it's this path which renaming manually within Explorer adds to the registry.
All I've managed to figure out so far is how to return the second path from a WMI (Win32_LogicalDisk -> ProviderName) call, but I really want to avoid the manual registry entry approach.
Upvotes: 13
Views: 4065
Reputation: 21
Here is my solution (i dont use C#), when i connect a server : net use z: http://example.com:8000 /user:user password
then windows show "DAVWWWRoot" in the explorer.
I add this in the registry to rename :
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##example.com@8000#DavWWWRoot" /v "_LabelFromReg" /t REG_SZ /d "Weytop Drive" /f
Explore the registry to see where you need to set this _LabelFromReg key.
Upvotes: 0
Reputation: 1
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:
\\www.myurl.com@SSL@5006\myFolder
##myurl.com@SSL@5006#myFolder
Shell32.Shell shell = new Shell32.Shell();
((Shell32.Folder2) shell.NameSpace("Z:")).Self.Name = "Shell";
Upvotes: 0
Reputation: 6527
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"}
Upvotes: 1