0

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.

Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14

2 Answers2

0

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
    }
}
shingo
  • 18,436
  • 5
  • 23
  • 42
  • Is it not possible to start the same process two times? yeah i mean its possible to ask if the process has got the right. but my main problem is that i need to start the process befor the installation with uac and after the installation without uac. – freeprogramming May 05 '22 at 13:39
  • Now if you run this program, you will see the UAC window, after you press the Yes button, it will run `Installation` and `AfterInstallation` step by step without UAC popuped. Isn't this what you want? – shingo May 05 '22 at 13:46
  • Will installation and afterinstallation run then with admin rights? – freeprogramming May 05 '22 at 13:48
  • Yes, they will. – shingo May 05 '22 at 13:49
  • And if im going to start again a process in afterinstallation for cmd will it run as admin too? or do i need the step with "runas" again and the uac? – freeprogramming May 05 '22 at 13:51
  • No, after a process gain the admin rights, other processes started from this process get this rights by default. – shingo May 05 '22 at 13:57
0

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.

Victor
  • 2,313
  • 2
  • 5
  • 13