Skip to content

Commit 100c21a

Browse files
committed
Add new api to run process as desktop user
1 parent e391c98 commit 100c21a

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,5 +580,17 @@ public interface IPublicAPI
580580
/// </summary>
581581
/// <returns>The time taken to execute the method in milliseconds</returns>
582582
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "");
583+
584+
/// <summary>
585+
/// Start a process with the given file path and arguments
586+
/// </summary>
587+
/// <remarks>
588+
/// It can help to start a deelevated process when Flow is running as administrator.
589+
/// </remarks>
590+
/// <param name="filePath">File path of the process to start. It can be an executable file or a script file</param>
591+
/// <param name="workingDirectory">Working directory of the process. If not specified, the current directory will be used</param>
592+
/// <param name="arguments">Optional arguments to pass to the process. If not specified, no arguments will be passed</param>
593+
/// <param name="runAsAdmin">Whether to run the process as administrator</param>
594+
public void StartProcess(string filePath, string workingDirectory, string arguments = "", bool runAsAdmin = false);
583595
}
584596
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
using System.Windows.Media;
1515
using CommunityToolkit.Mvvm.DependencyInjection;
1616
using Flow.Launcher.Core;
17+
using Flow.Launcher.Core.ExternalPlugins;
1718
using Flow.Launcher.Core.Plugin;
1819
using Flow.Launcher.Core.Resource;
19-
using Flow.Launcher.Core.ExternalPlugins;
2020
using Flow.Launcher.Core.Storage;
2121
using Flow.Launcher.Helper;
2222
using Flow.Launcher.Infrastructure;
23-
using Flow.Launcher.Infrastructure.Http;
2423
using Flow.Launcher.Infrastructure.Hotkey;
24+
using Flow.Launcher.Infrastructure.Http;
2525
using Flow.Launcher.Infrastructure.Image;
2626
using Flow.Launcher.Infrastructure.Logger;
2727
using Flow.Launcher.Infrastructure.Storage;
2828
using Flow.Launcher.Infrastructure.UserSettings;
2929
using Flow.Launcher.Plugin;
30-
using Flow.Launcher.Plugin.SharedModels;
3130
using Flow.Launcher.Plugin.SharedCommands;
31+
using Flow.Launcher.Plugin.SharedModels;
3232
using Flow.Launcher.ViewModel;
3333
using JetBrains.Annotations;
3434
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
@@ -577,6 +577,29 @@ public long StopwatchLogInfo(string className, string message, Action action, [C
577577
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "") =>
578578
Stopwatch.InfoAsync(className, message, action, methodName);
579579

580+
public void StartProcess(string filePath, string workingDirectory, string arguments = "", bool runAsAdmin = false)
581+
{
582+
// Deelevate process if it is running as administrator
583+
if (Win32Helper.IsAdministrator() && !runAsAdmin)
584+
{
585+
Win32Helper.RunAsDesktopUser(filePath, workingDirectory, arguments, out var errorInfo);
586+
if (!string.IsNullOrEmpty(errorInfo))
587+
{
588+
LogError(ClassName, $"Failed to start process {filePath} with error: {errorInfo}");
589+
}
590+
}
591+
592+
var info = new ProcessStartInfo
593+
{
594+
FileName = filePath,
595+
WorkingDirectory = workingDirectory,
596+
Arguments = arguments,
597+
UseShellExecute = true,
598+
Verb = runAsAdmin ? "runas" : "",
599+
};
600+
Process.Start(info)?.Dispose();
601+
}
602+
580603
#endregion
581604

582605
#region Private Methods

0 commit comments

Comments
 (0)