There is a code:

string command1 = comanda"; //Команда 1 // создаем процесс 1 System.Diagnostics.ProcessStartInfo procStartInfo1 = new System.Diagnostics.ProcessStartInfo("cmd" , st + command1); System.Diagnostics.Process pr1 = new System.Diagnostics.Process(); procStartInfo1.WindowStyle = ProcessWindowStyle.Hidden; pr1.StartInfo = procStartInfo; pr1.Start(); procStartInfo1.RedirectStandardOutput = true; procStartInfo1.UseShellExecute = false; procStartInfo1.CreateNoWindow = true; pr1.WaitForExit(); if (условие) { string command2 = comanda"; //Команда 2 // создаем процесс 2 System.Diagnostics.ProcessStartInfo procStartInfo2 = new System.Diagnostics.ProcessStartInfo("cmd" , st + command2); System.Diagnostics.Process pr2 = new System.Diagnostics.Process(); procStartInfo2.WindowStyle = ProcessWindowStyle.Hidden; pr2.StartInfo2 = procStartInfo2; pr2.Start(); procStartInfo2.RedirectStandardOutput = true; procStartInfo2.UseShellExecute = false; procStartInfo2.CreateNoWindow = true; pr2.WaitForExit(); } else { pr2.WaitForExit(); } 

this is how it works, but a new process is created for the second command, and it is necessary that the second command be executed on the very first process / cmd, how it is done, please tell me, try differently, but without a new process, it’s not like ...

    1 answer 1

    You can like this:

     Process pr1 = new Process(); var startInfo = new ProcessStartInfo("ping", "google.com"); //startInfo.WindowStyle = ProcessWindowStyle.Hidden; //startInfo.RedirectStandardOutput = true; //startInfo.UseShellExecute = false; //startInfo.CreateNoWindow = true; pr1.StartInfo = startInfo; pr1.Start(); pr1.WaitForExit(); if (true) { startInfo.Arguments = "2ip.ru"; pr1.Start(); pr1.WaitForExit(); } 

    Or like this:

     var command = "/C ping google.com"; if (true) { command += "& ping 2ip.ru"; } var startInfo = new ProcessStartInfo("cmd", command); //startInfo.WindowStyle = ProcessWindowStyle.Hidden; //startInfo.RedirectStandardOutput = true; //startInfo.UseShellExecute = false; //startInfo.CreateNoWindow = true; Process pr1 = Process.Start(startInfo); pr1.WaitForExit();