attempt to fix desktop icon creation

This commit is contained in:
DismissedLight
2023-10-03 17:39:30 +08:00
parent ecdaeccc42
commit 07f0c37b2c
3 changed files with 15 additions and 7 deletions

View File

@@ -5,5 +5,5 @@ namespace Snap.Hutao.Core.Shell;
internal interface IShellLinkInterop
{
bool TryCreateDesktopShoutcutForElevatedLaunch();
ValueTask<bool> TryCreateDesktopShoutcutForElevatedLaunchAsync();
}

View File

@@ -2,6 +2,8 @@
// Licensed under the MIT license.
using System.IO;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Win32;
using Windows.Win32.System.Com;
using Windows.Win32.UI.Shell;
@@ -15,15 +17,21 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop
{
private readonly RuntimeOptions runtimeOptions;
public bool TryCreateDesktopShoutcutForElevatedLaunch()
public async ValueTask<bool> TryCreateDesktopShoutcutForElevatedLaunchAsync()
{
string sourceLogoPath = Path.Combine(runtimeOptions.InstalledLocation, "Assets/Logo.ico");
Uri sourceLogoUri = "ms-appx:///Assets/Logo.ico".ToUri();
string targetLogoPath = Path.Combine(runtimeOptions.DataFolder, "ShellLinkLogo.ico");
try
{
// System.IO.IOException: 无法加密指定的文件。
File.Copy(sourceLogoPath, targetLogoPath, true);
StorageFile iconFile = await StorageFile.GetFileFromApplicationUriAsync(sourceLogoUri);
using (Stream inputStream = (await iconFile.OpenReadAsync()).AsStream())
{
using (FileStream outputStream = File.Create(targetLogoPath))
{
await inputStream.CopyToAsync(outputStream).ConfigureAwait(false);
}
}
}
catch
{

View File

@@ -268,9 +268,9 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel
}
[Command("CreateDesktopShortcutCommand")]
private void CreateDesktopShortcutForElevatedLaunch()
private async Task CreateDesktopShortcutForElevatedLaunch()
{
bool created = shellLinkInterop.TryCreateDesktopShoutcutForElevatedLaunch();
bool created = await shellLinkInterop.TryCreateDesktopShoutcutForElevatedLaunchAsync();
if (created)
{
infoBarService.Information(SH.ViewModelSettingActionComplete);