2013-02-25 162 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之內),包括所有參數。