From bf3e158d8ec5c1cd7266985cb81a74ec7ae2e9a5 Mon Sep 17 00:00:00 2001 From: Shaun Date: Sun, 18 Aug 2024 05:35:19 -0400 Subject: [PATCH] Return status code from elevated command --- NodeSwap/Commands/UseCommand.cs | 8 +++++--- NodeSwap/NodeSwap.csproj | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/NodeSwap/Commands/UseCommand.cs b/NodeSwap/Commands/UseCommand.cs index 7c35511..dc10760 100644 --- a/NodeSwap/Commands/UseCommand.cs +++ b/NodeSwap/Commands/UseCommand.cs @@ -64,8 +64,7 @@ public int Run() if (!IsAdministrator()) { // Restart the application with elevated privileges - ElevateApplication(); - return 1; + return ElevateApplication(); } // @@ -111,7 +110,7 @@ private static bool IsAdministrator() return principal.IsInRole(WindowsBuiltInRole.Administrator); } - private static void ElevateApplication() + private static int ElevateApplication() { var currentProcessModule = Process.GetCurrentProcess().MainModule; if (currentProcessModule == null) throw new Exception("Unable to get the current process module"); @@ -130,10 +129,13 @@ private static void ElevateApplication() try { process.Start(); + process.WaitForExit(); + return process.ExitCode; } catch (Exception ex) { Console.Error.WriteLine("Could not restart as Administrator: " + ex.Message); + return 1; } } diff --git a/NodeSwap/NodeSwap.csproj b/NodeSwap/NodeSwap.csproj index 74c15a8..8b9621d 100644 --- a/NodeSwap/NodeSwap.csproj +++ b/NodeSwap/NodeSwap.csproj @@ -4,7 +4,7 @@ Exe net8.0-windows false - 1.4.1 + 1.4.2 true win-x64