Skip to content

Commit ec314a4

Browse files
committed
Feature: Follow File Explorer naming preferences when creating shortcuts
1 parent ccc04ba commit ec314a4

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-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: 29 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,8 +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"));
616+
617+
var dest = source.Select(x => Path.Combine(destination, FilesystemHelpers.GetShortcutNamingPreference(x.Name)));
617618

618619
source = await source.ToListAsync();
619620
dest = await dest.ToListAsync();
@@ -671,8 +672,8 @@ public static bool IsValidForFilename(string name)
671672
if (string.IsNullOrEmpty(item.src.Path) || item.src.Path != item.dest)
672673
{
673674
// Same item names in both directories
674-
if (StorageHelpers.Exists(item.dest) ||
675-
(FtpHelpers.IsFtpPath(item.dest) &&
675+
if (StorageHelpers.Exists(item.dest) ||
676+
(FtpHelpers.IsFtpPath(item.dest) &&
676677
await Ioc.Default.GetRequiredService<IFtpStorageService>().TryGetFileAsync(item.dest) is not null))
677678
{
678679
(incomingItems[item.index] as FileSystemDialogConflictItemViewModel)!.ConflictResolveOption = FileNameConflictResolveOptionType.GenerateNewName;
@@ -797,7 +798,7 @@ public static async Task<IEnumerable<IStorageItemWithPath>> GetDraggedStorageIte
797798
catch (COMException)
798799
{
799800
}
800-
801+
801802
if (bytesRead > 0)
802803
{
803804
IntPtr dropStructPointer = Marshal.AllocHGlobal(dropBytes!.Length);
@@ -867,12 +868,34 @@ public static bool ContainsRestrictedFileName(string input)
867868
return false;
868869
}
869870

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

874897
// SUPPRESS: Cannot convert null literal to non-nullable reference type.
875-
#pragma warning disable CS8625
898+
#pragma warning disable CS8625
876899
associatedInstance = null;
877900
filesystemOperations = null;
878901
}

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

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

141-
var shortcutName = string.Format("ShortcutCreateNewSuffix".ToLocalized(), destinationName);
141+
var shortcutName = FilesystemHelpers.GetShortcutNamingPreference(destinationName);
142+
142143
ShortcutCompleteName = shortcutName + extension;
143144
var filePath = Path.Combine(WorkingDirectory, ShortcutCompleteName);
144145

@@ -152,4 +153,4 @@ private async Task CreateShortcutAsync()
152153
ShortcutCreatedSuccessfully = await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, DestinationItemPath);
153154
}
154155
}
155-
}
156+
}

0 commit comments

Comments
 (0)