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

Ami*_*Pal 32 c# cmd makecert netsh

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

我使用以下代码:

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 = 
...
Run Code Online (Sandbox Code Playgroud)

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

bas*_*h.d 44

它纯粹是一个字符串:

startInfo.Arguments = "-sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer"
Run Code Online (Sandbox Code Playgroud)

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

"... -ss \"My MyAdHocTestCert.cer\""
Run Code Online (Sandbox Code Playgroud)

有关此信息,请参阅MSDN.

  • 也许我不明白这个答案,但你的代码似乎只添加了一条指令,即OP提到的第一条指令。他们如何使用相同的 startInfo 添加第二条指令? (2认同)

小智 6

请记住包含 System.Diagnostics

ProcessStartInfo startInfo = new ProcessStartInfo("myfile.exe");        // exe file
startInfo.WorkingDirectory = @"C:\..\MyFile\bin\Debug\netcoreapp3.1\"; // exe folder

//here you add your arguments
startInfo.ArgumentList.Add("arg0");       // First argument          
startInfo.ArgumentList.Add("arg2");       // second argument
startInfo.ArgumentList.Add("arg3");       // third argument
Process.Start(startInfo);                 
Run Code Online (Sandbox Code Playgroud)

  • 对于空格分隔的参数或名称/值对,这是最好的答案。不过这里有一个警告。如果您需要像这样的参数:`--arg1="value1"`。这将被转义并且不会按预期工作。 (2认同)