c# - 在一行中执行多个 cmd 命令
问题描述
我有多个命令,我想使用 Process 在一行中执行它们。第一个命令是使用For loop
.
for /f ... do xxxx...
然后在完成上面的命令后执行另一个多个命令。
& [command] & [command] & [command] & [command]
我了解&
and&&
运算符的使用,所以我尝试了以下命令,但没有结果。
for /f ... do xxxx... & [command] & [command] & [command] & [command]
总结一下这个问题,问题是在完成后出现的for loop
,第二个命令没有执行,由于操作员的原因,应该执行有无错误&
。
我目前的解决方案是使用分离的进程方法,并在完成for loop
命令后调用。
private string void cmd_command(){
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/K";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(@"for /f ... do xxxx...");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
string response = process.StandardOutput.ReadToEnd();
Console.WriteLine(response);
if(response.ToUpperInvariant().Contains(expectedOutput)){
//call second multiple command
return next_cmd_command();
}
return null;
}
private string next_cmd_command(){
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/K";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(@"[command] & [command] & [command] & [command]");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
string response = process.StandardOutput.ReadToEnd();
Console.WriteLine(response);
return response;
}
解决方案
推荐阅读
- python - 替换与另一列熊猫中的特定值相对应的列中的空值
- axapta - 使用特定域控制器从 Dynamics AX 读取 Active Directory 中的用户信息
- javascript - 为什么我的图像仅在调试模式下可见
- javascript - 我无法从其他函数调用值
- java - 尝试使用 Postman 导致内存不足异常
- ios - 如何修复“必需的”初始化程序必须可以在类“BeAlertController”可以被子类化的地方访问”
- c# - 负数左移,先到先得
- php - PDO SELECT 未完成或引发异常
- xamarin.forms - 如何阻止切换事件触发导航
- html - 将标题文本保持在与背景容器相关的相同位置