C# 使用参数和来自C的多个命令执行.exe#

C# 使用参数和来自C的多个命令执行.exe#,c#,C#,我目前正试图“转换”我写给C#的bash脚本。 此脚本在shell中启动一个程序,然后在此shell中执行一些命令,看起来类似于: cd /$MYPATH ./executible -s << EOF start_source_probe -hardware_name "USB" -device_name "@1: EP3C(10|5)" write_source_data -instance_index 0 -value "11111" write_source_data -ins

我目前正试图“转换”我写给C#的bash脚本。 此脚本在shell中启动一个程序,然后在此shell中执行一些命令,看起来类似于:

cd /$MYPATH
./executible -s << EOF
start_source_probe -hardware_name "USB" -device_name "@1: EP3C(10|5)"
write_source_data -instance_index 0 -value "11111"
write_source_data -instance_index 0 -value "10111"
write_source_data -instance_index 0 -value "00111"
exit
EOF
在注释被激活的情况下(代码中的“/”),我成功地打开了shell(-s代表shell),但我想知道如何在这个shell中执行命令。 我用类似的方法执行了多个命令(只要我没有启动shell,因为我想目标输出是不同的)


如果有人能告诉我如何添加参数“-s”并在启动的过程中执行命令,我将不胜感激。

我不确定是否理解您的问题,但您可以在c代码之外创建一个批处理文件,并从c代码调用它,如下所示:

System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo("cmd");
ProcStartInfo.RedirectStandardOutput = true;
ProcStartInfo.UseShellExecute = false;
ProcStartInfo.CreateNoWindow = false;
ProcStartInfo.RedirectStandardError = true;
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
ProcStartInfo.Arguments = "/c start /wait batch.bat ";
MyProcess.StartInfo = ProcStartInfo;
MyProcess.Start();
MyProcess.WaitForExit();

我添加了
/wait
,因此您的c#代码将等待批处理完成,为了继续执行c#代码

,我发现使用cmd.exe执行多个命令最麻烦不过了。最好将命令写入.cmd文件并执行。你的.cmd文件中会有“正常”的批处理语法。你能告诉我如何在这里添加一个文档,比如“我不理解你的问题,你说的“在我的bash脚本中添加一个这样的文档”和“在批处理脚本中添加”是什么意思,或者“不再重要了。我知道了怎么做。”键入sometext.txt | myexe.exe-s”他说。谢谢你的回答!
const string strCmdText = "/C command1&command2";
Process.Start("CMD.exe", strCmdText);
System.Diagnostics.ProcessStartInfo ProcStartInfo = new System.Diagnostics.ProcessStartInfo("cmd");
ProcStartInfo.RedirectStandardOutput = true;
ProcStartInfo.UseShellExecute = false;
ProcStartInfo.CreateNoWindow = false;
ProcStartInfo.RedirectStandardError = true;
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
ProcStartInfo.Arguments = "/c start /wait batch.bat ";
MyProcess.StartInfo = ProcStartInfo;
MyProcess.Start();
MyProcess.WaitForExit();