How to run command in the process which is execute

2019-08-28 01:47发布

问题:

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 = ,