7

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
    }
}
Marco
  • 56,740
  • 14
  • 129
  • 152
Paxamime
  • 203
  • 2
  • 7
  • 1
    Setting the same volume label on *all* removable drives cannot (should not) what you have in mind. Don't go altering drive data willy-nilly. Get the go-ahead from the user. – Hans Passant May 13 '11 at 23:14
  • It would not be done "willy-nilly" it would actually be done by the user (me) pressing a button. It is to automate renaming drives so I don't have to do it manually hundreds of times. – Paxamime May 13 '11 at 23:20

2 Answers2

5

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");
}
MatSnow
  • 7,357
  • 3
  • 19
  • 31
Paxamime
  • 203
  • 2
  • 7
2

Have you tried DriveInfo.VolumeLabel?

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

James Kovacs
  • 11,549
  • 40
  • 44