I want to open Windows command prompt from C# code and want to execute batch file from it containing few commands. I tried with simple "echo" and "dir" command on trial basis and it is not working. I am neither getting error nor expected output. At least cmd window should open then arguments can be passed.
I tried two options - runnind cmd.exe and running batch file. I am not able to find the mistake I am making in the code. Thanks in advance for the help.
var baseDirectory = "~/App_Data/TestRoot";
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.UseShellExecute = false;
processStartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath(baseDirectory);
processStartInfo.CreateNoWindow = false;
processStartInfo.WindowStyle = ProcessWindowStyle.Maximized;
//running cmd.exe then .bat as argument
processStartInfo.FileName = HttpContext.Current.Server.MapPath(baseDirectory + "/cmd.exe");
processStartInfo.Arguments = HttpContext.Current.Server.MapPath(baseDirectory + "/test.bat");
Process process = Process.Start(processStartInfo);
I changed FileName and removed Arguments property for option second, please refer code below.
processStartInfo.FileName = HttpContext.Current.Server.MapPath(baseDirectory + "/test.bat");
The test.bat file contains just one line echo hello. When I tried to run this directly from CMD and double click, it is working fine in both the cases. I am facing issue only in case of invoking it from C# code.
I am not going to get any result in output stream. That batch file will run a process in background, which won't return any output in stream. This is not duplicate question. Please stop marking this as duplicate.