Browse Source

Combine Elevated MoveFiles, handling improvements

pull/55/head
Ionite 1 year ago
parent
commit
f9e1c7c520
No known key found for this signature in database
  1. 12
      StabilityMatrix.Avalonia/Helpers/WindowsElevated.cs
  2. 3
      StabilityMatrix.Avalonia/Helpers/WindowsShortcuts.cs
  3. 39
      StabilityMatrix.Avalonia/ViewModels/SettingsViewModel.cs

12
StabilityMatrix.Avalonia/Helpers/WindowsElevated.cs

@ -1,7 +1,7 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using StabilityMatrix.Core.Processes;
namespace StabilityMatrix.Avalonia.Helpers;
@ -11,13 +11,15 @@ public static class WindowsElevated
/// <summary>
/// Move a file from source to target using elevated privileges.
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
public static async Task<int> MoveFile(string sourcePath, string targetPath)
public static async Task<int> MoveFiles(params (string sourcePath, string targetPath)[] moves)
{
// Combine into single command
var args = string.Join(" & ", moves.Select(
x => $"move \"{x.sourcePath}\" \"{x.targetPath}\""));
using var process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/c move \"{sourcePath}\" \"{targetPath}\"";
process.StartInfo.Arguments = $"/c {args}";
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "runas";

3
StabilityMatrix.Avalonia/Helpers/WindowsShortcuts.cs

@ -18,6 +18,7 @@ public static class WindowsShortcuts
string iconPath,
string description)
{
// ReSharper disable once SuspiciousTypeConversion.Global
var link = (IShellLink) new ShellLink();
// setup shortcut information
@ -25,7 +26,7 @@ public static class WindowsShortcuts
link.SetPath(targetPath);
link.SetIconLocation(iconPath, 0);
// save it
// ReSharper disable once SuspiciousTypeConversion.Global
var file = (IPersistFile) link;
file.Save(shortcutPath, false);
}

39
StabilityMatrix.Avalonia/ViewModels/SettingsViewModel.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
@ -12,6 +13,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentAvalonia.UI.Controls;
using NLog;
using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Helpers;
using StabilityMatrix.Avalonia.Models;
using StabilityMatrix.Avalonia.Services;
@ -181,6 +183,21 @@ public partial class SettingsViewModel : PageViewModelBase
return;
}
// Confirmation dialog
var dialog = new BetterContentDialog
{
Title = "This will create a shortcut for Stability Matrix in the Start Menu for all users",
Content = "You will be prompted for administrator privileges. Continue?",
PrimaryButtonText = "Yes",
CloseButtonText = "Cancel",
DefaultButton = ContentDialogButton.Primary
};
if (await dialog.ShowAsync() != ContentDialogResult.Primary)
{
return;
}
await using var _ = new MinimumDelay(200, 300);
var shortcutDir = new DirectoryPath(
@ -200,20 +217,22 @@ public partial class SettingsViewModel : PageViewModelBase
"Stability Matrix");
// Move to target
var moveLinkResult = await WindowsElevated.MoveFile(
tempDir.JoinFile("Stability Matrix.lnk"), shortcutLink);
try
{
var moveLinkResult = await WindowsElevated.MoveFiles(
(tempDir.JoinFile("Stability Matrix.lnk"), shortcutLink),
(tempDir.JoinFile("Stability Matrix.ico"), iconPath));
if (moveLinkResult != 0)
{
notificationService.Show("Failed to create shortcut", $"Could not copy shortcut to {shortcutLink}");
return;
notificationService.ShowPersistent("Failed to create shortcut", $"Could not copy shortcut",
NotificationType.Error);
}
// Move icon
var moveIconResult = await WindowsElevated.MoveFile(
tempDir.JoinFile("Stability Matrix.ico"), iconPath);
if (moveIconResult != 0)
}
catch (Win32Exception e)
{
notificationService.Show("Failed to create shortcut", $"Could not copy icon to {iconPath}");
// We'll get this exception if user cancels UAC
Logger.Warn(e, "Could not create shortcut");
notificationService.Show("Could not create shortcut", "", NotificationType.Warning);
}
}

Loading…
Cancel
Save