Skip to content

Feature: Use File Explorer naming preferences when creating shortcuts #14600

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 4 commits into from
Feb 5, 2024
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
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/UI/UIFilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public static async Task CreateShortcutAsync(IShellPage? associatedInstance, IRe

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

if (!await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, selectedItem.ItemPath))
Expand Down
34 changes: 28 additions & 6 deletions src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Files.Core.Storage;
using Files.Core.Storage.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
Expand Down Expand Up @@ -612,9 +613,8 @@ public async Task<ReturnResult> CreateShortcutFromClipboard(DataPackageView pack
progress.ProgressChanged += (s, e) => returnStatus = returnStatus < ReturnResult.Failed ? e.Status!.Value.ToStatus() : returnStatus;

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

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

Expand Down Expand Up @@ -671,8 +671,8 @@ public static bool IsValidForFilename(string name)
if (string.IsNullOrEmpty(item.src.Path) || item.src.Path != item.dest)
{
// Same item names in both directories
if (StorageHelpers.Exists(item.dest) ||
(FtpHelpers.IsFtpPath(item.dest) &&
if (StorageHelpers.Exists(item.dest) ||
(FtpHelpers.IsFtpPath(item.dest) &&
await Ioc.Default.GetRequiredService<IFtpStorageService>().TryGetFileAsync(item.dest) is not null))
{
(incomingItems[item.index] as FileSystemDialogConflictItemViewModel)!.ConflictResolveOption = FileNameConflictResolveOptionType.GenerateNewName;
Expand Down Expand Up @@ -797,7 +797,7 @@ public static async Task<IEnumerable<IStorageItemWithPath>> GetDraggedStorageIte
catch (COMException)
{
}

if (bytesRead > 0)
{
IntPtr dropStructPointer = Marshal.AllocHGlobal(dropBytes!.Length);
Expand Down Expand Up @@ -867,12 +867,34 @@ public static bool ContainsRestrictedFileName(string input)
return false;
}


/// <summary>
/// Gets the shortcut naming template from File Explorer
/// </summary>
public static string GetShortcutNamingPreference(string itemName)
{
var keyName = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\NamingTemplates";
var value = Registry.GetValue(keyName, "ShortcutNameTemplate", null);

if (value is null)
return string.Format("ShortcutCreateNewSuffix".GetLocalizedResource(), itemName) + ".lnk";
else
{
// Trim the quotes and the "%s" from the string
value = value?.ToString()?.TrimStart(['"', '%', 's']);
value = value?.ToString()?.TrimEnd(['"']);

return itemName + value;
}
}


public void Dispose()
{
filesystemOperations?.Dispose();

// SUPPRESS: Cannot convert null literal to non-nullable reference type.
#pragma warning disable CS8625
#pragma warning disable CS8625
associatedInstance = null;
filesystemOperations = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private async Task CreateShortcutAsync()
destinationName = uri.Host;
}

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

Expand All @@ -152,4 +152,4 @@ private async Task CreateShortcutAsync()
ShortcutCreatedSuccessfully = await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, DestinationItemPath);
}
}
}
}