Skip to content

Commit a60a629

Browse files
authored
Feature: Added support for renaming network drives (#13050)
1 parent c740d57 commit a60a629

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/Files.App/Data/Items/DriveItem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root
166166

167167
item.Text = type switch
168168
{
169-
DriveType.Network => $"{root.DisplayName} ({deviceId})",
170169
DriveType.CDRom when !string.IsNullOrEmpty(label) => root.DisplayName.Replace(label.Left(32), label),
171170
_ => root.DisplayName
172171
};

src/Files.App/Utils/Shell/Win32API.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,15 @@ public static void OpenFormatDriveDialog(string drive)
511511
RunPowershellCommand($"-command \"$Signature = '[DllImport(\\\"shell32.dll\\\", SetLastError = false)]public static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint options);'; $SHFormatDrive = Add-Type -MemberDefinition $Signature -Name \"Win32SHFormatDrive\" -Namespace Win32Functions -PassThru; $SHFormatDrive::SHFormatDrive(0, {driveIndex}, 0xFFFF, 0x0001)\"", true);
512512
}
513513

514-
public static void SetVolumeLabel(string driveName, string newLabel)
514+
public static void SetVolumeLabel(string drivePath, string newLabel)
515515
{
516516
// Rename requires elevation
517-
RunPowershellCommand($"-command \"$Signature = '[DllImport(\\\"kernel32.dll\\\", SetLastError = false)]public static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);'; $SetVolumeLabel = Add-Type -MemberDefinition $Signature -Name \"Win32SetVolumeLabel\" -Namespace Win32Functions -PassThru; $SetVolumeLabel::SetVolumeLabel('{driveName}', '{newLabel}')\"", true);
517+
RunPowershellCommand($"-command \"$Signature = '[DllImport(\\\"kernel32.dll\\\", SetLastError = false)]public static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);'; $SetVolumeLabel = Add-Type -MemberDefinition $Signature -Name \"Win32SetVolumeLabel\" -Namespace Win32Functions -PassThru; $SetVolumeLabel::SetVolumeLabel('{drivePath}', '{newLabel}')\"", true);
518+
}
519+
520+
public static void SetNetworkDriveLabel(string driveName, string newLabel)
521+
{
522+
RunPowershellCommand($"-command \"(New-Object -ComObject Shell.Application).NameSpace('{driveName}').Self.Name='{newLabel}'\"", false);
518523
}
519524

520525
public static bool MountVhdDisk(string vhdPath)

src/Files.App/Views/Properties/GeneralPage.xaml.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ bool SaveDrive(DriveItem drive)
8484

8585
newName = letterRegex.Replace(newName, string.Empty); // Remove "(C:)" from the new label
8686

87-
Win32API.SetVolumeLabel(drive.Path, newName);
87+
if (drive.Type == Data.Items.DriveType.Network)
88+
Win32API.SetNetworkDriveLabel(drive.DeviceID, newName);
89+
else
90+
Win32API.SetVolumeLabel(drive.Path, newName);
91+
8892
_ = MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
8993
{
9094
await drive.UpdateLabelAsync();

0 commit comments

Comments
 (0)