1

I want to start a process as admin.
The admin creditials are hard coded so that users without knowing the admin creditials could open the process as admin.

I have tried

var process = new ProcessStartInfo 
{
    FileName = "path",
    UserName = "userName",
    Domain = "myDomain",
    Password = mySecritPassword,
    UseShellExecute = false,
    Verb = "runas",
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true
 };

 Process.Start(process);

But my process is starting without admin rights

Is there something i forgot?

csk
  • 73
  • 1
  • 7
  • Why does the process need to run as administrator? Since you're allowing any non-admin to cause it to run, can you not instead adjust the permissions required on whatever objects you're accessing? – Damien_The_Unbeliever Oct 16 '18 at 09:49
  • Possible duplicate of [Elevating process privilege programmatically?](https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically) – BWA Oct 16 '18 at 09:50
  • @Damien_The_Unbeliever the thing is that i want to start a third party application that needs admin rights. Its not only one application i want to start, a list of applications thats allways changing – csk Oct 16 '18 at 10:56

1 Answers1

0

You need to make UseShellExecute true. So this should work:

var process = new ProcessStartInfo 
{
    FileName = "path",
    UserName = "userName",
    Domain = "myDomain",
    Password = mySecritPassword,
    UseShellExecute = true,
    Verb = "runas",
    RedirectStandardError = true,
    RedirectStandardInput = true,
    RedirectStandardOutput = true
 };

 Process.Start(process);

You need to accept the UAC prompt to gain admin privileges, and for some reason, the UAC prompt is only shown when you use ShellExecute. For more information (but not much), see this article: Teach Your Apps To Play Nicely With Windows Vista User Account Control.

Bas
  • 1,946
  • 21
  • 38
  • 2
    Thanks for your answere. The problem is that if I set ``UseShellExcecute = true`` i get an exception that tells me that I need to set ``UseShellExcecute = false`` if i want to start a process as a different user – csk Oct 16 '18 at 10:52
  • I did some digging and apparently, there's no simple way to do this, but it is possible. See here: https://blogs.msdn.microsoft.com/cjacks/2010/02/01/why-cant-i-elevate-my-application-to-run-as-administrator-while-using-createprocesswithlogonw/ – Bas Oct 16 '18 at 11:31