C# 如何在processStartInfo中传递多个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15061854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:55:26  来源:igfitidea点击:

How to pass multiple arguments in processStartInfo?

c#cmdnetshmakecert

提问by Amit Pal

I want to run some cmdcommand from c#code. I followed some blogs and tutorial and got the answer, but I am in little bit confused i.e how should I pass multiple arguments?

我想cmdc#代码运行一些命令。我关注了一些博客和教程并得到了答案,但我有点困惑,即我应该如何传递多个参数?

I use follow code:

我使用以下代码:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = 
...

What will be the startInfo.Argumentsvalue for the following command line code?

startInfo.Arguments以下命令行代码的值是多少?

  • makecert -sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer

  • netsh http add sslcert ipport=127.0.0.1:8085 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable

  • makecert -sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer

  • netsh http add sslcert ipport=127.0.0.1:8085 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable

采纳答案by bash.d

It is purely a string:

它纯粹是一个字符串:

startInfo.Arguments = "-sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer"

Of course, when arguments contain whitespaces you'll have to escape them using \" \", like:

当然,当参数包含空格时,您必须使用 \" \" 将它们转义,例如:

"... -ss \"My MyAdHocTestCert.cer\""

See MSDNfor this.

有关此信息,请参阅MSDN

回答by Martheen

For makecert, your startInfo.FileNameshould be the complete path of makecert (or just makecert.exe if it's in standard path) then the Argumentswould be -sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cernow I'm bit unfamiliar with how certificate store works, but perhaps you'll need to set startInfo.WorkingDirectoryif you're referring the .cer files outside the certificate store

对于makecert,您startInfo.FileName应该makecert的完整路径(或只是makecert.exe如果它在标准路径),那么Arguments将是-sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer现在我和证书存储如何工作有点陌生,但也许你需要一套startInfo.WorkingDirectory,如果你”重新引用证书存储区之外的 .cer 文件

回答by Zaid Amir

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/c -sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer"

use /c as a cmd argument to close cmd.exe once its finish processing your commands

使用 /c 作为 cmd 参数以在完成处理命令后关闭 cmd.exe

回答by sparky68967

startInfo.Arguments = "/c \"netsh http add sslcert ipport=127.0.0.1:8085 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable\"";

and...

和...

startInfo.Arguments = "/c \"makecert -sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer\"";

The /ctells cmd to quit once the command has completed. Everything after /cis the command you want to run (within cmd), including all of the arguments.

/c告诉CMD退出,一旦命令完成。后面的所有内容都是/c您要运行的命令(在 内cmd),包括所有参数。