5

I am trying to run a cmd command with the following code:

ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process exec = Process.Start(cmd);
exec.StandardInput.WriteLine("sc create \"BaliService\" binPath= \"{0}\\BaliService.exe\"", Directory.GetCurrentDirectory());

This command requires admin privelages, if I run cmd as administrator and type the command it works perfectly but not when I run this app as admin. I have added

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

to a manifest file which prompts uac each time I open the exe.

I have seen multiple questions on this and they all seem to suggest any processes run under an elevated app will have the same rights but this isn't working for me.

I have tried cmd.Verb = "runas"; but no dice.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Try changing `cmd.UseShellExecute` to `true` and keep the `runas` verb. – keyboardP Dec 31 '11 at 21:09
  • Just tried it but got this error `The Process object must have the UseShellExecute property set to false in order to redirect IO streams.` Thanks. – Bali C Dec 31 '11 at 21:11
  • 4
    That's where the buck stops, you cannot have it both ways. UAC prevents an unelevated process from hi-jacking the capabilities of an elevated one. The only way you can redirect is to elevate yourself first. – Hans Passant Dec 31 '11 at 23:24

2 Answers2

13

You need to set UseShellExecute to true for the Verb to be respected and it must be set to 'false' to redirect standard output. You can't do both.

I'm pretty sure Windows also won't allow you to redirect standard input/output/error across the admin/non-admin security boundary. You'll have to find a different way to get output from the program running as admin.

I didn't read this article, but this may give you more information: http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx

shf301
  • 31,086
  • 2
  • 52
  • 86
  • I try using UseShellExecute = false; Verb = "runas"; RedirectStandardInput = true; Domain = du[0]; UserName = UserAdministrator; Password = SecureStringHelper.ToSecureString(pwd); LoadUserProfile = true; And using requestedExecutionLevel in manifest. If I use UseShellExecute = true; I get the error The Process object must have the UseShellExecute property set to false in order to start a process as a user. – Kiquenet Aug 22 '14 at 11:10
  • Full sample different way to get output from the program running as admin ? – Kiquenet Aug 28 '14 at 06:31
2

Did you try assigning administrative credentials to your ProcessStartInfo object?

SecureString password = new SecureString();
password.AppendChar('p');
password.AppendChar('w');
cmd.UserName = "admin";
cmd.Password = password;
Abbas
  • 6,720
  • 4
  • 35
  • 49
  • I can't do this as I need UAC to prompt for them as it will be used on multiple PC's, thanks. – Bali C Dec 31 '11 at 21:21
  • Okay, makes sense. Can you tell me why you are creating a command process and directing it to execute "sc" rather then running the "sc" command directly ? – Abbas Dec 31 '11 at 21:27
  • I am trying to run the sc command through a button on a winform as the user won't know how to use sc, just to make it easy for them to use the program. – Bali C Jan 01 '12 at 11:35
  • What I am asking is why aren't you running 'sc' directly i.e. cmd.FileName = "cmd.exe" and cmd.Arguments = String.Format("create \"BaliService\" binPath= \"{0}\\BaliService.exe\"", Directory.GetCurrentDirectory()). – Abbas Jan 01 '12 at 21:59
  • @Abbas: this solution was working for me until Win10, now the new process will not be elevated when the orignal process wasn't elevated itself. Do you have an idea for this? – eFloh Nov 12 '15 at 13:25