Set Drive VolumeLabel

好久不见. 提交于 2019-12-10 13:18:37

问题


I am working on a small utility where I would like to change the volume label on flash drives that are connected to the computer. I know that DriveInfo is capable of doing it but I am at a loss as for how to accomplish it. If anyone has a code sample I would really appreciate it.
Here is what i currently have:

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

回答1:


Thanks James! I don't know why I had so many issues with this but you got me going down the right path.

Here is the final code to set the drive label. For anyone else that uses this, it will change the name of ANY removable drive that is attached to the system. If you need to only change the names of specific drive models you can use WMI's Win32_DiskDrive to narrow it down.

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



回答2:


Have you tried DriveInfo.VolumeLabel?

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



来源:https://stackoverflow.com/questions/5998535/set-drive-volumelabel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!