Start the elevated process

I am trying to run 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 an administrator and type a command that works fine, but not when I run this application as an administrator. I added

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

to the manifest file that uac requests every time I open exe.

I saw several questions about this, and they all seem to assume that any processes running under the elevated application will have the same rights, but this does not work for me.

I tried cmd.Verb = "runas"; but not a cube.

+5
c # process
Dec 31 '11 at 21:05
source share
2 answers

You need to set UseShellExecute to true for Verb , and to redirect it to standard output must be set to false. You cannot get around both.

I am sure that Windows will also not allow you to redirect standard input / output / error to the admin / non-admin security boundary. You will need to find another way to get the result from a program running as admin.

I have not read this article, but it may give you additional information: http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx

+13
Dec 31 '11 at 21:29
source share

Have you tried to assign administrative credentials to your ProcessStartInfo object?

 SecureString password = new SecureString(); password.AppendChar('p'); password.AppendChar('w'); cmd.UserName = "admin"; cmd.Password = password; 
+2
Dec 31 '11 at 21:20
source share



All Articles