Elevating privileges doesn't work with UseShellExecute=false
ProcessStartInfo.Verb will only have an effect if the process is started by ShellExecuteEx(). Which requires UseShellExecute = true. Redirecting I/O and hiding the window can only work if the process is started by CreateProcess(). Which requires UseShellExecute = false.
Well, that's why it doesn't work. Not sure if forbidding to start a hidden process that bypasses UAC was intentional. Probably. Very probably.
Check this Q+A for the manifest you need to display the UAC elevation prompt.
Allow UAC window when CreateNoWindow is set
Instead of starting the 'target' app directly like this, instead create a small 'helper' app that does nothing but start the target app with CreateNoWindow.
Then you start your 'helper' app with 'runas' and without CreateNoWindow, since your 'helper' app won't create any windows that's not a problem, and since it was run with 'runas' it'll be able to run other elevated programs without 'runas' itself - and can therefore use CreateNoWindow.
C# Start Program with ADMIN rights from other program
Try adding this line
process.StartInfo.UseShellExecute = true;
on
using (var process = new Process())
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}
Redirect standard output and prompt for UAC with ProcessStartInfo
UseShellExecute
must be set to false
to redirect IO, and to true
to use the Verb
property. So you can't.
But this article seems do the magic, although I haven't tested it.
It's written in C++, but a wrapper API can easily be created to be called from C# by using DllImport
.
Note: If you want to pass data between the two programs and have access to the target program's source code, you can easily re-design you application to use Named Pipes
instead of redirecting standard I/O.
Verb = runas not run as elevated
Process.Start() can use the OS or Shell (Explorer.exe) to spawn a new process, but only the Shell will prompt for elevation.
Thus you have to specify UseShellExecute=true in ProcessStartInfo, according to this answer: processstartinfo-verb-runas-not-working
UseShellExecute=false will allow you to capture Standard Output and Standard Error messages.
Related Topics
C# How to Loop While Mouse Button Is Held Down
Does Garbage Collection Run During Debug
Replace Unicode Escape Sequences in a String
How to Implement Gzip Compression in ASP.NET
+= New Eventhandler(Method) VS += Method
How to Know the Size of the String in Bytes
How to List All Loaded Assemblies
How to Use a Controller in Another Assembly in ASP.NET Core MVC 2.0
Httpwebrequest Not Passing Credentials
How to Clear All Controls on a Form C#
How to "Zip" or "Rotate" a Variable Number of Lists
Why Does Visual Studio Type a Newly Minted Array as Nullable
Handling the Window Closing Event with Wpf/Mvvm Light Toolkit
Deferred Execution and Eager Evaluation
How to "Await Yield Return Dosomethingasync()"
Declaring a Variable Inside or Outside an Foreach Loop: Which Is Faster/Better
How to Tell the Data Annotations Validator to Also Validate Complex Child Properties