0

Using C# console we could invoke current cmd/bat files. But it always popup new command line window.

I've tried invoking my cmd/bat files one by one and could not known how to running them in the same window. Pretty simple as below:

static void Main(string[] args)
{
    Process.Start("first.cmd").WaitForExit(); // popup a new window

    Process.Start("second.cmd").WaitForExit(); // popup a new window

    Process.Start("third.bat").WaitForExit(); // popup a new window

    Console.WriteLine("Press any key to exit ... ");
    Console.ReadKey();
}

Is any way to start all them in current windown? How to void new popup?

David Smith
  • 893
  • 2
  • 9
  • 19
  • 6
    If this is literally your program in its entirety, you could replace it with a wrapper `.bat` file instead and it'll just use one window when calling the others. – Damien_The_Unbeliever Jan 28 '19 at 07:41
  • You can do "Process proc = new Process();" and then set the "proc.StartInfo.CreateNoWindow" to your desired state – MindSwipe Jan 28 '19 at 07:50

2 Answers2

1

This is how to do it:

static void Main(string[] args)
{
    ExecuteProcesses(new string[] { "test1.bat", "test2.bat", "test3.bat" });
}

public static void ExecuteProcesses(string[] executablesPaths)
{
    using (Process process = new Process())
    {
        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.UseShellExecute = false;
        process.StartInfo = processStartInfo;
        foreach (string executablePath in executablesPaths)
        {
            process.StartInfo.FileName = executablePath;
            process.Start();
            Console.WriteLine(process.StandardOutput.ReadToEnd());
            process.WaitForExit();
        }
    }
}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

Try invoking only a single process with the different commands as parameters to the CMD.

static void Main(string[] args)
{
    ProcessStartInfo pStartInfo = new ProcessStartInfo
    {
        FileName = "CMD",
        Arguments = @"/C echo 1 && echo 2 && echo 3"
    }
    Process.Start(pStartInfo).WaitForExit();

    Console.WriteLine("Press any key to exit ... ");
    Console.ReadKey();
}