Add new api to run process as desktop user

This commit is contained in:
Jack251970 2025-06-11 16:02:24 +08:00
parent e391c98474
commit 100c21a83e
2 changed files with 38 additions and 3 deletions

View file

@ -580,5 +580,17 @@ namespace Flow.Launcher.Plugin
/// </summary>
/// <returns>The time taken to execute the method in milliseconds</returns>
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "");
/// <summary>
/// Start a process with the given file path and arguments
/// </summary>
/// <remarks>
/// It can help to start a deelevated process when Flow is running as administrator.
/// </remarks>
/// <param name="filePath">File path of the process to start. It can be an executable file or a script file</param>
/// <param name="workingDirectory">Working directory of the process. If not specified, the current directory will be used</param>
/// <param name="arguments">Optional arguments to pass to the process. If not specified, no arguments will be passed</param>
/// <param name="runAsAdmin">Whether to run the process as administrator</param>
public void StartProcess(string filePath, string workingDirectory, string arguments = "", bool runAsAdmin = false);
}
}

View file

@ -14,21 +14,21 @@ using System.Windows;
using System.Windows.Media;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core;
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Core.ExternalPlugins;
using Flow.Launcher.Core.Storage;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.Http;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using Flow.Launcher.Plugin.SharedModels;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Plugin.SharedModels;
using Flow.Launcher.ViewModel;
using JetBrains.Annotations;
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
@ -577,6 +577,29 @@ namespace Flow.Launcher
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "") =>
Stopwatch.InfoAsync(className, message, action, methodName);
public void StartProcess(string filePath, string workingDirectory, string arguments = "", bool runAsAdmin = false)
{
// Deelevate process if it is running as administrator
if (Win32Helper.IsAdministrator() && !runAsAdmin)
{
Win32Helper.RunAsDesktopUser(filePath, workingDirectory, arguments, out var errorInfo);
if (!string.IsNullOrEmpty(errorInfo))
{
LogError(ClassName, $"Failed to start process {filePath} with error: {errorInfo}");
}
}
var info = new ProcessStartInfo
{
FileName = filePath,
WorkingDirectory = workingDirectory,
Arguments = arguments,
UseShellExecute = true,
Verb = runAsAdmin ? "runas" : "",
};
Process.Start(info)?.Dispose();
}
#endregion
#region Private Methods