如何设置卷标?

时间:2013-07-23 07:43:02

标签: c#

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命令创建的虚拟驱动器的卷标?

3 个答案:

答案 0 :(得分:1)

你不能这样做。

来自the Microsoft documentation for SUBST

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 

答案 1 :(得分:0)

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

答案 2 :(得分: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/以获取更多帮助。 如果状态为false,请参阅此链接,以了解error

相关问题