简体   繁体   English

C#如何通过管理员提升的cmd运行进程(带参数)

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

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#. 我使用上面的代码在C#中通过cmd运行一个进程。

However, the problem is: 但问题是:

  1. "processNeedToRun" needs arguments when running. “processNeedToRun”在运行时需要参数。
  2. Even i set Verb ="runas", Windows 7 still prompts an elevate dialog. 即使我设置了Verb =“runas”,Windows 7仍然会提示提升对话框。

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 . 如果启动processNeedToRun的进程未提升,则无法避免提升对话框 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. 但是,向processNeedToRun添加参数没有问题。 You can just add them to the arguments you pass to ProcessStartInfo : 您可以将它们添加到传递给ProcessStartInfo的参数中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM