Following problem: I want to start the cmd as administrator in C# with a process to get the UAC befor the code:
var process = new Process();
var ps= new ProcessStartInfo();
ps.CreateNoWindow = true;
ps.FileName = @"cmd.exe";
ps.Verb = "runas";
process.Start();
After i confirm the UAC, i want to run code. After the code i want to start the same process but this time with arguments and without the UAC. Is that possible? Target is that i get a exe with the running code. I need the exe to run the arguments in the cmd. But the UAC should pop up before the exe will be appear. (the code should run after the uac, and the arguments after code). PS: I need the administrator priviliges to run the arguments.
CodePudding user response:
You can elevate the program self first. If the process has got the right, run the wix installer. You can find IsUserAdministrator
here.
public static void Main()
{
var isAdmin = IsUserAdministrator();
if(isAdmin)
{
Installation();
AfterInstallation();
}
else
{
var p = new Process();
var si = p.StartInfo;
si.CreateNoWindow = true;
si.UseShellExecute = true;
si.FileName = Process.GetCurrentProcess().MainModule.FileName;
si.Verb = "runas";
p.Start(); // UAC will show here only once
}
}
CodePudding user response:
I have limited knowledge about this but I think you can:
- Start your application (InstanceA) without admin.
- From InstanceA, run the application with admin (InstanceB). You can run InstanceB with a /Install parameter and InstaceB knows that must start the installation.
- Install the application from InstanceB.
- InstanceA can wait until installations is finished and then, do whatever you want.
You may need some type of communication (pipes, memory mapping, sockets...) between both instances or something what you can monitor from InstanceA (registry key...) to check if the installations is completed and if you want more control about the whole process.