This time, this is a pretty interesting question; I up-voted it.
I can offer much better solution. Here is the thing: in all techniques using
System.Diagnostics.Process
, there is nothing more pointless than using "CMD.EXE". I see no cases when it really needs to be started programmatically. It's purpose is just to provide interactive shell. And the worst feature is "/C" command-line argument: it works, but is useless, only provoke naive users to use it.
Now, at first glance, it looks like your interesting question justifies the use of "CMD.EXE /C …" But this is nothing by illusion. Why? Because '&' trick is nothing but a trick. Some other application would do it much better. It can transparently pass command line parameters, any number, to a set of separate
Process.Start
calls, solving the problem completely.
But I understand you major problem: this application should be elevated, but your main application should not. You provided good motivation for that requirement. Besides, one extra application is always a hassle. I'm going to address both problems, too.
You cannot elevate the current process. But it does not mean that you cannot elevate
the same application in a different process. In other words, develop your main application in the following way: normally, it is not elevated and does not provide the "elevation required" manifest. But this main application can start
another process using the same application assembly as elevated one. For short period of time, you will have two processes started from the same application: parent one is not elevated, and child one elevated. I think you know how to elevated the process using
System.Diagnostic.Process
: start it with
System.Diagnostics.ProcessStartInfo
with
Verb
"RunAs":
ProcessStartInfo.Verb Property (System.Diagnostics)[
^],
ProcessStartInfo.Verbs Property (System.Diagnostics)[
^],
Process.Start Method (ProcessStartInfo) (System.Diagnostics)[
^].
On the use of "runas", see, for example,
c# - Elevating process privilege programatically? — Stack Overflow[
^].
So, essentially, you can use the same .NET application assembly as two functionally different applications: one used as the main one, another to be elevated and start other processes. How to distinguish the two during process's runtime: the "elevated one" can use "special" command-line parameters.
For work with command-line parameters, I can offer my easy-to-use library:
Enumeration-based Command Line Utility[
^].
—SA