2013-02-25 413 views
24

我想从c#代码运行一些cmd命令。我跟着一些博客和教程,得到了答案,但我有点混乱,即如何传递多个参数?如何在processStartInfo中传递多个参数?

我使用如下代码:

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

什么将是下面的命令行代码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

回答

32

这纯粹是一个字符串:

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

当然,当参数包含空格,您必须使用逃脱他们\ “\”,如:

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

请参阅MSDN

+0

如果我需要使用|像这个命令中的符号? netstat -ano | find/i“聆听”| find/i“17328” – Revious 2015-03-06 13:14:20

+0

我的猜测是要逃避“通过使用”,给它一个镜头。 – 2015-03-09 13:18:47

+0

也许我不明白这个答案,但是你的代码似乎只添加一条指令,这是OP提到的第一条指令。他们怎么可能用相同的startInfo添加他们的第二条指令? – ThePartyTurtle 2016-07-01 15:03:10

0

对于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文件证书店外

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

使用/ C作为cmd参数关闭CMD.EXE一旦其处理完你的命令

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

和...

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

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