JT
1 year ago
13 changed files with 477 additions and 178 deletions
@ -0,0 +1,32 @@
|
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Runtime.Versioning; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Helpers; |
||||
|
||||
[SupportedOSPlatform("windows")] |
||||
public static class WindowsElevated |
||||
{ |
||||
/// <summary> |
||||
/// Move a file from source to target using elevated privileges. |
||||
/// </summary> |
||||
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 {args}"; |
||||
process.StartInfo.UseShellExecute = true; |
||||
process.StartInfo.CreateNoWindow = true; |
||||
process.StartInfo.Verb = "runas"; |
||||
|
||||
process.Start(); |
||||
await process.WaitForExitAsync().ConfigureAwait(false); |
||||
|
||||
return process.ExitCode; |
||||
} |
||||
} |
@ -0,0 +1,64 @@
|
||||
using System; |
||||
using System.Diagnostics.CodeAnalysis; |
||||
using System.Runtime.InteropServices; |
||||
using System.Runtime.InteropServices.ComTypes; |
||||
using System.Runtime.Versioning; |
||||
using System.Text; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Helpers; |
||||
|
||||
[SupportedOSPlatform("windows")] |
||||
[SuppressMessage("ReSharper", "IdentifierTypo")] |
||||
[SuppressMessage("ReSharper", "InconsistentNaming")] |
||||
public static class WindowsShortcuts |
||||
{ |
||||
public static void CreateShortcut( |
||||
string shortcutPath, |
||||
string targetPath, |
||||
string iconPath, |
||||
string description) |
||||
{ |
||||
// ReSharper disable once SuspiciousTypeConversion.Global |
||||
var link = (IShellLink) new ShellLink(); |
||||
|
||||
// setup shortcut information |
||||
link.SetDescription(description); |
||||
link.SetPath(targetPath); |
||||
link.SetIconLocation(iconPath, 0); |
||||
|
||||
// ReSharper disable once SuspiciousTypeConversion.Global |
||||
var file = (IPersistFile) link; |
||||
file.Save(shortcutPath, false); |
||||
} |
||||
|
||||
[ComImport] |
||||
[Guid("00021401-0000-0000-C000-000000000046")] |
||||
private class ShellLink |
||||
{ |
||||
} |
||||
|
||||
[ComImport] |
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
||||
[Guid("000214F9-0000-0000-C000-000000000046")] |
||||
private interface IShellLink |
||||
{ |
||||
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags); |
||||
void GetIDList(out IntPtr ppidl); |
||||
void SetIDList(IntPtr pidl); |
||||
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); |
||||
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); |
||||
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); |
||||
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); |
||||
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); |
||||
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); |
||||
void GetHotkey(out short pwHotkey); |
||||
void SetHotkey(short wHotkey); |
||||
void GetShowCmd(out int piShowCmd); |
||||
void SetShowCmd(int iShowCmd); |
||||
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon); |
||||
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); |
||||
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); |
||||
void Resolve(IntPtr hwnd, int fFlags); |
||||
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); |
||||
} |
||||
} |
@ -0,0 +1,15 @@
|
||||
namespace StabilityMatrix.Core.Models.FileInterfaces; |
||||
|
||||
public class TempDirectoryPath : DirectoryPath, IDisposable |
||||
{ |
||||
public TempDirectoryPath() : base(Path.GetTempPath(), Path.GetRandomFileName()) |
||||
{ |
||||
Directory.CreateDirectory(FullPath); |
||||
} |
||||
|
||||
public void Dispose() |
||||
{ |
||||
Directory.Delete(FullPath, true); |
||||
GC.SuppressFinalize(this); |
||||
} |
||||
} |
Loading…
Reference in new issue