从文本框打开命令并将其写入 cmd
本文关键字:cmd 命令 文本 | 更新日期: 2023-09-27 18:34:17
我正在尝试打开一个cmd.exe
并从多个文本框中写入。但是除了cmd之外,我什么都不会出现:
System.Diagnostics.Process.Start("cmd", "perl "+ textBox5.Text + textBox4.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text);
您需要
使用选项 /c
启动任一cmd
,并使用 cmd /c "perl ...
等"
传递每个后续数据,或者您可以直接启动perl
作为进程并将其他所有内容作为参数传递。
您可以在此处找到有关参数的详细文档。
因此,您必须将代码更改为
System.Diagnostics.Process.Start("cmd","/c '"perl "+ textBox5.Text + textBox4.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text + "'"");
或
System.Diagnostics.Process.Start("perl", textBox5.Text + textBox4.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text);
此外:您可以通过不将+
与strings
结合使用来提高代码的可读性和性能。如果要使用 StringBuilder,则可以将代码更改为以下代码:
StringBuilder arguments = new StringBuilder();
arguments.Append(textBox5.Text);
arguments.Append(textBox4.Text);
arguments.Append(textBox6.Text);
arguments.Append(textBox7.Text);
arguments.Append(textBox8.Text);
arguments.Append(textBox9.Text);
System.Diagnostics.Process.Start("perl", arguments.ToString());
你应该在参数的开头添加参数/c 或/k
http://ss64.com/nt/cmd.html