开发者

C# how to run a process(with arguments) through an administrator elevated cmd

开发者 https://www.devze.com 2023-02-03 23:21 出处:网络
ProcessStartInfo procStartInfo = new ProcessStartInfo(\"cmd\", \"/c \" +\"processNeedToRun\") { RedirectStandardError = true,
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " +"processNeedToRun")
{
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    Verb ="runas"
};

I use the above code to run a process through cmd in C#.

However, the problem is:

  1. "processNeedToRun" needs a开发者_StackOverflowrguments when running.
  2. Even i set Verb ="runas", Windows 7 still prompts an elevate dialog.

Is it possible to meet all the requirements?


If the process that's launching processNeedToRun is not elevated, then there is no way to avoid the elevation dialog. Doing so would be a security hole. So you're just going to have to live with the elevation prompt.

Adding arguments to processNeedToRun is no problem, though. You can just add them to the arguments you pass to ProcessStartInfo:

var procStartInfo = new ProcessStartInfo("cmd", "/c processNeedToRun arg1 arg2");
0

精彩评论

暂无评论...
验证码 换一张
取 消