7

我正在开发一个小型实用程序,我想在其中更改连接到计算机的闪存驱动器上的卷标。我知道 DriveInfo 有能力做到这一点,但我不知道如何完成它。如果有人有代码示例,我将不胜感激。
这是我目前拥有的:

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
    if (d.IsReady && d.DriveType == DriveType.Removable)
    {
        //set volume label here
    }
}
4

2 回答 2

5

谢谢詹姆斯!我不知道为什么我有这么多问题,但你让我走上了正确的道路。

这是设置驱动器标签的最终代码。对于使用它的任何其他人,它将更改连接到系统的任何可移动驱动器的名称。如果您只需要更改特定驱动器型号的名称,您可以使用 WMIWin32_DiskDrive来缩小范围。

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");
}
于 2011-05-16T20:04:22.743 回答
2

你试过 DriveInfo.VolumeLabel 吗?

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel.aspx

于 2011-05-13T22:57:04.337 回答