如何在processStartInfo中传递多个参数

本文关键字:参数 processStartInfo | 更新日期: 2023-09-27 18:01:10

我想从c#代码中运行一些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

如何在processStartInfo中传递多个参数

它纯粹是一个字符串:

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。

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完成对命令的处理后关闭

且。。。
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中(,包括所有参数。

记住包括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);                 

只需使用"amp"在您的命令行中

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

.NetStandard 2.1包含一个名为ArgumentList的不错功能,当给定Collection<string>时,它会自动为您转义参数。但是(就像我的情况一样(如果你不能瞄准.NetStandard 2.1,你就运气不好了。。。但是!我深入研究了.NetStandard 2.1ProcessStartInfo源代码,并能够提取出这个类:

internal static class PasteArguments {
        internal static void AppendArgument(StringBuilder stringBuilder, string argument) {
            // from https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/PasteArguments.cs
            if (stringBuilder.Length != 0) {
                stringBuilder.Append(' ');
            }
            // Parsing rules for non-argv[0] arguments:
            //   - Backslash is a normal character except followed by a quote.
            //   - 2N backslashes followed by a quote ==> N literal backslashes followed by unescaped quote
            //   - 2N+1 backslashes followed by a quote ==> N literal backslashes followed by a literal quote
            //   - Parsing stops at first whitespace outside of quoted region.
            //   - (post 2008 rule): A closing quote followed by another quote ==> literal quote, and parsing remains in quoting mode.
            if (argument.Length != 0 && ContainsNoWhitespaceOrQuotes(argument)) {
                // Simple case - no quoting or changes needed.
                stringBuilder.Append(argument);
            } else {
                stringBuilder.Append(Quote);
                int idx = 0;
                while (idx < argument.Length) {
                    char c = argument[idx++];
                    if (c == Backslash) {
                        int numBackSlash = 1;
                        while (idx < argument.Length && argument[idx] == Backslash) {
                            idx++;
                            numBackSlash++;
                        }
                        if (idx == argument.Length) {
                            // We'll emit an end quote after this so must double the number of backslashes.
                            stringBuilder.Append(Backslash, numBackSlash * 2);
                        } else if (argument[idx] == Quote) {
                            // Backslashes will be followed by a quote. Must double the number of backslashes.
                            stringBuilder.Append(Backslash, numBackSlash * 2 + 1);
                            stringBuilder.Append(Quote);
                            idx++;
                        } else {
                            // Backslash will not be followed by a quote, so emit as normal characters.
                            stringBuilder.Append(Backslash, numBackSlash);
                        }
                        continue;
                    }
                    if (c == Quote) {
                        // Escape the quote so it appears as a literal. This also guarantees that we won't end up generating a closing quote followed
                        // by another quote (which parses differently pre-2008 vs. post-2008.)
                        stringBuilder.Append(Backslash);
                        stringBuilder.Append(Quote);
                        continue;
                    }
                    stringBuilder.Append(c);
                }
                stringBuilder.Append(Quote);
            }
        }

示例用法:

static string GetArgumentStr(List<string> argList) {
        if(argList == null || argList.Count == 0) {
            return string.Empty;
        }
        var sb = new StringBuilder();
        foreach(var arg in argList) {
            PasteArguments.AppendArgument(sb, arg);
        }
        return sb.ToString();
    }