C#:使用多个参数启动进程,其中任何一个参数都等于textbox.text?

Gi0*_*Gi0 0 c# arguments process

Windows窗体应用程序,从按钮调用命令提示符.我设法通过以下方式传递多个参数

Process execute = new Process();
execute.StartInfo.WorkingDirectory = minusstring; //string equal to textBox1.Text
execute.StartInfo.FileName = @"cmd";
execute.StartInfo.Arguments = @"-log d:file.txt -c ""arg2"" -y ""arg3"" -z ""HOW?""";
execute.Start()
Run Code Online (Sandbox Code Playgroud)

我想问的是用textBox2.Text值填充-z参数(我的代码中的HOW?)的方法,该值由用户填充.用户填写的值是本地驱动器中文件的路径,例如c:\ Folder\foo.txt

忍受我和可能的愚蠢错误,我刚开始学习.

Jus*_*ner 6

我会使用String.Format来适当地格式化参数字符串:

execute.StartInfo.Arguments = 
    String.Format("-log d:file.txt -c \"arg2\" -y \"arg3\" -z \"{0}\"",
                  textBox1.Text);
Run Code Online (Sandbox Code Playgroud)

您也可以只进行简单的字符串连接,如果您只有一个要替换的参数,它将正常工作.如果您需要可扩展到更多参数的东西,请坚持使用String.Format().