简体   繁体   English

如何在以管理员权限执行的过程中运行命令?

[英]How to run command in the process which is executed with admin rights?

I want to create a self signed certificate and install it using through c# program. 我想创建一个自签名证书,并通过c#程序安装它。 I use makecert to make certificate i run it as administrator and i pass command in the ProcessStartInfo.argument but the command doesn't executes what is the problem in the code? 我使用makecert来制作证书,我以管理员身份运行它,并在ProcessStartInfo.argument中传递命令,但是该命令未执行代码中的问题是什么?

Here is my code: 这是我的代码:

 public void Createasnewadmin()
 {

        ProcessStartInfo info = new ProcessStartInfo();

        Process p = new Process();          

        info.FileName = Application.StartupPath+@"\makecert.exe";

        info.UseShellExecute = true;

        info.Verb = "runas"; // Provides Run as Administrator

        info.Arguments = "makecert testCert_admin_check.cer";

        //i just create sample certificate but it doesn't get created
        //The problem is above line the command doesn't get execute 

        p.StartInfo=info;

        p.Start()

  }

Please Tell me where is the problem is it not executing as administrator? 请告诉我问题出在哪里,它没有以管理员身份执行? or the command to be executed is not passed properly? 或要执行的命令未正确传递?

I think it is executing as admin as i myself click on yes button to execute as admin that is prompted by windows 我认为它以管理员身份执行,就像我本人一样,单击“是”按钮以Windows提示以管理员身份执行

Why is command not executing? 为什么命令不执行? is there any other way? 还有其他办法吗?

Taking a look at your code, I suspect you are getting an error because your arguments are incorrect. 看一下您的代码,我怀疑您遇到错误,因为您的参数不正确。

You line 你行

info.Arguments = "makecert testCert_admin_check.cer"; 

should be 应该

info.Arguments = "testCert_admin_check.cer"; 

I believe you need to supply credentials to invoke process in admin mode. 我相信您需要提供凭据才能以管理方式调用进程。

UserName = "Administrator", Password = , 用户名=“管理员”,密码=,

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

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