Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Drive VolumeLabel

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
    }
}
like image 879
Paxamime Avatar asked May 13 '11 22:05

Paxamime


People also ask

Can you change volume label?

How to Change the Volume Label. Renaming a volume is easy to do from both Command Prompt and through File Explorer or Disk Management. Open Disk Management and right-click the drive you want renamed. Choose Properties and then, in the General tab, erase what's there and type what you'd prefer it to be.

How do I change the volume label in Windows 10?

Change drive label using File ExplorerOpen File Explorer. Click on This PC from the left pane. Under the “Devices and drives” section, right-click the drive and select the Rename option. Specify a new label for the drive and press Enter.


1 Answers

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");
}
like image 95
Paxamime Avatar answered Oct 05 '22 20:10

Paxamime