Reputation: 77
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");
}
But, It's not working with virtual drive. how to change volume label of virtual drive created by SUBST command?
Upvotes: 1
Views: 2852
Reputation: 11
Try following code
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);
public static void Main()
{
string name = "E:\\";
var status = SetVolumeLabel(name, "Test");
var error = Marshal.GetLastWin32Error();
Console.WriteLine(status + " " + error);
}
refer http://www.pinvoke.net/ for more help. refer this link if status is false, to understand error
Upvotes: 1
Reputation: 1799
On my system I am unable to change the volume label of a virtual drive even though the drive it is created from has no drive label, but there is a "for display only" workaround that works on my Windows 7 machine: Open My Computer, click on the virtual drive to highlight it, then wait a second, then click once on the drive letter. This will give you the option to rename the drive by typing something in and hitting enter. This normally will change the drive label, but because you aren't able to change the drive label of a virtual drive Windows instead creates this registry key: HKEY_USERS\{your SID}\Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\{drive letter}\DefaultLabel
to display the label to you. This DOES NOT change the drive label, it only allows you to give a sort of display name to the virtual drive in explorer. But for some people this may be all they need.
Upvotes: 0
Reputation: 109597
You can't do this.
From the Microsoft documentation for SUBST:
The following commands do not work, or should not be used,
on drives used in the subst command:
chkdsk
diskcomp
diskcopy
format
label <------- NOTE
recover
Upvotes: 2