Skip to content

Commit

Permalink
Return status code from elevated command
Browse files Browse the repository at this point in the history
  • Loading branch information
simshaun committed Aug 18, 2024
1 parent 03f1a22 commit bf3e158
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions NodeSwap/Commands/UseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public int Run()
if (!IsAdministrator())
{
// Restart the application with elevated privileges
ElevateApplication();
return 1;
return ElevateApplication();
}

//
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion NodeSwap/NodeSwap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Version>1.4.1</Version>
<Version>1.4.2</Version>
<!-- Single file app – https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file -->
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down

0 comments on commit bf3e158

Please # to comment.