加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 设置Drive VolumeLabel

发布时间:2020-12-15 07:58:18 所属栏目:百科 来源:网络整理
导读:我正在开发一个小实用程序,我想更改连接到计算机的闪存驱动器上的卷标.我知道DriveInfo能够做到这一点,但我不知道如何实现它.如果有人有代码示例我会非常感激. 这是我目前拥有的: DriveInfo[] allDrives = DriveInfo.GetDrives();foreach (DriveInfo d in a
我正在开发一个小实用程序,我想更改连接到计算机的闪存驱动器上的卷标.我知道DriveInfo能够做到这一点,但我不知道如何实现它.如果有人有代码示例我会非常感激.
这是我目前拥有的:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
    if (d.IsReady && d.DriveType == DriveType.Removable)
    {
        //set volume label here
    }
}

解决方法

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

以下是设置驱动器标签的最终代码.对于使用此功能的任何其他人,它将更改连接到系统的任何可移动驱动器的名称.如果只需要更改特定驱动器型号的名称,可以使用WMI的Win32_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");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读