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

て烟熏妆下的殇ゞ 提交于 2019-12-11 15:20:46

问题


I want to create a self signed certificate and install it using through c# program. 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?

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

Why is command not executing? is there any other way?


回答1:


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



回答2:


I believe you need to supply credentials to invoke process in admin mode.

UserName = "Administrator", Password = ,



来源:https://stackoverflow.com/questions/7620008/how-to-run-command-in-the-process-which-is-executed-with-admin-rights

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