Skip to content

Feature: Added support for renaming network drives #13050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Files.App/Data/Items/DriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root

item.Text = type switch
{
DriveType.Network => $"{root.DisplayName} ({deviceId})",
DriveType.CDRom when !string.IsNullOrEmpty(label) => root.DisplayName.Replace(label.Left(32), label),
_ => root.DisplayName
};
Expand Down
9 changes: 7 additions & 2 deletions src/Files.App/Utils/Shell/Win32API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,15 @@ public static void OpenFormatDriveDialog(string drive)
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);
}

public static void SetVolumeLabel(string driveName, string newLabel)
public static void SetVolumeLabel(string drivePath, string newLabel)
{
// Rename requires elevation
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);
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);
}

public static void SetNetworkDriveLabel(string driveName, string newLabel)
{
RunPowershellCommand($"-command \"(New-Object -ComObject Shell.Application).NameSpace('{driveName}').Self.Name='{newLabel}'\"", false);
}

public static bool MountVhdDisk(string vhdPath)
Expand Down
6 changes: 5 additions & 1 deletion src/Files.App/Views/Properties/GeneralPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ bool SaveDrive(DriveItem drive)

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

Win32API.SetVolumeLabel(drive.Path, newName);
if (drive.Type == Data.Items.DriveType.Network)
Win32API.SetNetworkDriveLabel(drive.DeviceID, newName);
else
Win32API.SetVolumeLabel(drive.Path, newName);

_ = MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
await drive.UpdateLabelAsync();
Expand Down