Skip to content

Commit 3e3aacb

Browse files
authored
Feature: Use File Explorer naming preferences when creating shortcuts (#14600)
1 parent 21c722b commit 3e3aacb

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/Files.App/Helpers/UI/UIFilesystemHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public static async Task CreateShortcutAsync(IShellPage? associatedInstance, IRe
380380

381381
foreach (ListedItem selectedItem in selectedItems)
382382
{
383-
var fileName = string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), selectedItem.Name) + ".lnk";
383+
var fileName = FilesystemHelpers.GetShortcutNamingPreference(selectedItem.Name);
384384
var filePath = Path.Combine(currentPath ?? string.Empty, fileName);
385385

386386
if (!await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath))

src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Files.Core.Storage;
55
using Files.Core.Storage.Extensions;
66
using Microsoft.Extensions.Logging;
7+
using Microsoft.Win32;
78
using System.IO;
89
using System.Runtime.InteropServices;
910
using System.Runtime.InteropServices.ComTypes;
@@ -612,9 +613,8 @@ public async Task<ReturnResult> CreateShortcutFromClipboard(DataPackageView pack
612613
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;
613614

614615
source = source.Where(x => !string.IsNullOrEmpty(x.Path));
615-
var dest = source.Select(x => Path.Combine(destination,
616-
string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), x.Name) + ".lnk"));
617616

617+
var dest = source.Select(x => Path.Combine(destination, FilesystemHelpers.GetShortcutNamingPreference(x.Name)));
618618
source = await source.ToListAsync();
619619
dest = await dest.ToListAsync();
620620

@@ -671,8 +671,8 @@ public static bool IsValidForFilename(string name)
671671
if (string.IsNullOrEmpty(item.src.Path) || item.src.Path != item.dest)
672672
{
673673
// Same item names in both directories
674-
if (StorageHelpers.Exists(item.dest) ||
675-
(FtpHelpers.IsFtpPath(item.dest) &&
674+
if (StorageHelpers.Exists(item.dest) ||
675+
(FtpHelpers.IsFtpPath(item.dest) &&
676676
await Ioc.Default.GetRequiredService<IFtpStorageService>().TryGetFileAsync(item.dest) is not null))
677677
{
678678
(incomingItems[item.index] as FileSystemDialogConflictItemViewModel)!.ConflictResolveOption = FileNameConflictResolveOptionType.GenerateNewName;
@@ -797,7 +797,7 @@ public static async Task<IEnumerable<IStorageItemWithPath>> GetDraggedStorageIte
797797
catch (COMException)
798798
{
799799
}
800-
800+
801801
if (bytesRead > 0)
802802
{
803803
IntPtr dropStructPointer = Marshal.AllocHGlobal(dropBytes!.Length);
@@ -867,12 +867,34 @@ public static bool ContainsRestrictedFileName(string input)
867867
return false;
868868
}
869869

870+
871+
/// <summary>
872+
/// Gets the shortcut naming template from File Explorer
873+
/// </summary>
874+
public static string GetShortcutNamingPreference(string itemName)
875+
{
876+
var keyName = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\NamingTemplates";
877+
var value = Registry.GetValue(keyName, "ShortcutNameTemplate", null);
878+
879+
if (value is null)
880+
return string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), itemName) + ".lnk";
881+
else
882+
{
883+
// Trim the quotes and the "%s" from the string
884+
value = value?.ToString()?.TrimStart(['"', '%', 's']);
885+
value = value?.ToString()?.TrimEnd(['"']);
886+
887+
return itemName + value;
888+
}
889+
}
890+
891+
870892
public void Dispose()
871893
{
872894
filesystemOperations?.Dispose();
873895

874896
// SUPPRESS: Cannot convert null literal to non-nullable reference type.
875-
#pragma warning disable CS8625
897+
#pragma warning disable CS8625
876898
associatedInstance = null;
877899
filesystemOperations = null;
878900
}

src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private async Task CreateShortcutAsync()
138138
destinationName = uri.Host;
139139
}
140140

141-
var shortcutName = string.Format("ShortcutCreateNewSuffix".ToLocalized(), destinationName);
141+
var shortcutName = FilesystemHelpers.GetShortcutNamingPreference(destinationName);
142142
ShortcutCompleteName = shortcutName + extension;
143143
var filePath = Path.Combine(WorkingDirectory, ShortcutCompleteName);
144144

@@ -152,4 +152,4 @@ private async Task CreateShortcutAsync()
152152
ShortcutCreatedSuccessfully = await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, DestinationItemPath);
153153
}
154154
}
155-
}
155+
}

0 commit comments

Comments
 (0)