【问题标题】:How to pass multiple arguments in processStartInfo?如何在 processStartInfo 中传递多个参数?
【发布时间】:2014-09-09 19:40:50
【问题描述】:

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

我使用以下代码:

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

【问题讨论】:

    标签: c# cmd netsh makecert


    【解决方案1】:

    纯粹是一个字符串:

    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

    【讨论】:

    • 如果我需要使用 |这个命令中的符号? netstat -ano |find /i "监听" |find /i "17328"
    • 我的猜测是通过使用 \" 来逃避 ",试一试。
    • 也许我不明白这个答案,但您的代码似乎只添加了一条指令,即 OP 提到的第一条指令。他们如何使用相同的 startInfo 添加第二条指令?
    【解决方案2】:
    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 完成处理您的命令后关闭它

    【讨论】:

      【解决方案3】:
      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 内),包括所有参数。

      【讨论】:

        【解决方案4】:

        记得包含 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);                 
        

        【讨论】:

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

        对于 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 现在我有点不熟悉证书存储的工作方式,但如果您在证书存储之外引用 .cer 文件,则可能需要设置 startInfo.WorkingDirectory

        【讨论】:

          【解决方案6】:

          只需在命令行中使用“&&”即可。

           StartInfo.Arguments = @"/C cd C:\Users\yoooo\Desktop && echo This is a sample text file > sample.txt" 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-11-21
            • 2012-06-12
            • 1970-01-01
            • 2019-03-21
            • 2015-02-17
            相关资源
            最近更新 更多