How to use process.start runas in WinUI 3?

600 views Asked by At

When I tried to use code below to launch a high privilege process from a low one,a uac popout and the something.exe(C# console app with some dll) stucks without giving any output or error, and ocupying quite some cpu.Everything is fine if I run the Something.exe by right clicking it and run as administrator.Why??? I have tried Fody.Costura or set the workingdirectory but the problem wasn't solved(seems not a dll issue). I am using .net core6 and windows app sdk 1.2.230217.4

Process P = new Process();
P.StartInfo.UseShellExecute = true;
P.StartInfo.Verb = "runas";
P.StartInfo.FileName = ApplicationHelper.GetFullPathToExe() + "\\Win64\\SomeThing.exe";
P.StartInfo.Arguments = msg;
P.Start();
1

There are 1 answers

1
Andrew KeepCoding On BEST ANSWER

This is what worked for me:

  1. Create a plain console app named "Something.exe".
  2. Manually copy "Something.exe" folder, "net6.0" in my case, to the WinUI 3 app's LocalState folder. (You can automatically do this with MSBuild Copy task.)
  3. In your WinUI 3 app call this code:
// This is the path for the "LocalState" folder.
string directory = Windows.Storage.ApplicationData.Current.LocalFolder.Path;

Process P = new();
P.StartInfo.UseShellExecute = true;
P.StartInfo.Verb = "runas";
P.StartInfo.FileName = $@"{directory}\net6.0\Something.exe";
P.StartInfo.Arguments = "";
P.Start();