1

I was working on a project which needs to run two cmd commands on C#, I looked up on how to do it but nothing worked for me, and everyone was giving the solution to execute only one cmd command.

I would like to execute one command when the user clicks a button, then another one right after, without quitting the cmd, and if possible to hide the cmd window.

My goal would be to execute the 2 following commands in the cmd, to run a Run.bat file:

cd C:\users\user\documents\file
Run.bat

Thanks.

  • why not write a batch file that executes both for you? Then you just call the one bat file. – Mikael Jul 01 '20 at 19:38
  • 1
    Perhaps [this will help](https://stackoverflow.com/a/8055390/1390548). You can run multiple commands separated by `&&`. And please do show the work you have done in running one command at least. [See this](https://stackoverflow.com/a/206347/1390548) to find code on how to execute from c#. – Jawad Jul 01 '20 at 19:40

1 Answers1

2

You can directly call "C:\users\user\documents\file\Run.bat" and set the working directory as well as the shell execute flag :

string path = @"C:\users\user\documents\file\";
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = path + "Run.bat";
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
process.Start();
//process.WaitForExit();