Install VolumeLabel Drive

I am working on a small utility where I would like to change the volume label on the flash drives that are connected to the computer. I know that DriveInfo can do this, but I don’t understand how to do it. If anyone has some sample code, I would really appreciate it. Here is what I have now:

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

Thanks to James! I don’t know why I had so many problems with this, but you made me go the right way.

Here is the final code to set the drive label. For those who use this, it will change the name of ANY removable disk attached to the system. If you only need to change the names of specific drive models, you can use WMI 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"); } 
+4
source

Source: https://habr.com/ru/post/888082/


All Articles