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.
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"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With