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

半腔热情 提交于 2019-12-11 08:33:54

问题


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 arguments when running.
  2. Even i set Verb ="runas", Windows 7 still prompts an elevate dialog.

Is it possible to meet all the requirements?


回答1:


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");


来源:https://stackoverflow.com/questions/4687214/c-sharp-how-to-run-a-processwith-arguments-through-an-administrator-elevated-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!