1
public void SetVolumeLabel(string newLabel)
{
    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo d in allDrives)
    {
        if (d.IsReady && d.DriveType == DriveType.Removable)
        {
            d.VolumeLabel = newLabel;
        }
    }
}

public string VolumeLabel { get; set; }

// Setting the drive name
private void button1_Click(object sender, EventArgs e)
{
    SetVolumeLabel("FlashDrive");
}

但是,它不适用于虚拟驱动器。如何更改由 SUBST 命令创建的虚拟驱动器的卷标?

4

3 回答 3

2

你不能这样做。

来自SUBST 的 Microsoft 文档

The following commands do not work, or should not be used,
on drives used in the subst command:

chkdsk 
diskcomp 
diskcopy 
format 
label <------- NOTE
recover 
于 2013-07-23T07:51:11.747 回答
0

在我的系统上,即使创建它的驱动器没有驱动器标签,我也无法更改虚拟驱动器的卷标,但有一个“仅用于显示”的解决方法适用于我的 Windows 7 机器:打开我的电脑,单击虚拟驱动器以突出显示它,然后等待一秒钟,然后单击驱动器号一次。这将为您提供通过输入内容并按 Enter 来重命名驱动器的选项。这通常会更改驱动器标签,但由于您无法更改虚拟驱动器的驱动器标签,Windows 会创建此注册表项:HKEY_USERS\{your SID}\Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\{drive letter}\DefaultLabel向您显示标签。这不会更改驱动器标签,它只允许您为资源管理器中的虚拟驱动器提供一种显示名称。但对于某些人来说,这可能就是他们所需要的。

于 2015-07-20T19:50:25.400 回答
0

试试下面的代码

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);
    public static void Main()
    {
        string name = "E:\\";
        var status = SetVolumeLabel(name, "Test");
        var error = Marshal.GetLastWin32Error();
        Console.WriteLine(status + " " + error);
    }

请参阅http://www.pinvoke.net/以获得更多帮助。如果状态为假,请参考此链接,以了解错误

于 2016-09-14T14:51:11.540 回答