如何以编程方式更改映射驱动器的标签? [英] How do I programmatically change the label of a mapped drive?

查看:29
本文介绍了如何以编程方式更改映射驱动器的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 WNetAddConnection2 API.以防万一,这是一个 WebDAV 驱动器,而不是普通的 SMB 共享.

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.

网上的一些答案推荐使用 System.IO.DriveType,例如:

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";
    }
}

这显然不适用于网络驱动器,并且由 MSDN,其中声明将抛出 UnauthorizedAccessException 异常.

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.

其次,我尝试使用shell方法:

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

上面的代码创建了一个看起来正确的密钥,并添加了一个 _LabelFromReg REG_SZ,其值为Shell".但是,这不会反映在资源管理器或其他任何地方.

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".

这样做会在 MountPoints2 中创建一个新的、略有不同的键,它完美地工作.

Doing so creates a new, slightly different key within MountPoints2 which works perfectly.

所以,上面的 shell 代码并没有完全正确地解析路径 - 还有什么我可以尝试的吗?以上让我相信 Windows 必须在内部使用不同的 API 调用来重命名驱动器?

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?

更新 1

这肯定与这些 WebDAV 驱动器有关.一定有一些幕后处理正在进行.

This is definitely related to these being WebDAV drives. There must be some under-the-hood processing going on.

我的软件映射 https://myurl.com@ssl/stuff/destination/docs.可以使用 Net Use 命令查看该确切路径.上面的 shell 代码将这个路径添加到注册表中并尝试命名.

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.

但是,将鼠标悬停在 Windows 资源管理器中的驱动器上会给我 https://myurl.com@ssl/anotherfolder/stuff/destination 并且这是在资源管理器中手动重命名添加到注册表的路径.

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.

到目前为止,我设法弄清楚的是如何从 WMI (Win32_LogicalDisk -> ProviderName) 调用返回第二条路径,但我真的想避免手动注册表项方法.

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.

推荐答案

您可以在 C# 代码中使用 PowerShell,https://msdn.microsoft.com/en-us/library/system.management.automation.powershell(v=vs.85).aspx

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

使用 PowerShell 将 DriveLetter E 更改为 Q

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"}

这篇关于如何以编程方式更改映射驱动器的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆