0

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.

Rita
  • 1
  • 2
  • 4
    have a look at [How To: Execute command line in C#, get STD OUT results](https://stackoverflow.com/a/206347/2417602) – vikscool Mar 20 '19 at 10:37
  • 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. – Rita Mar 20 '19 at 11:09
  • @Sentry I am not getting expected output from specified code in "How to execute.." question. This is not duplicate question. – Rita Mar 20 '19 at 11:39
  • @vdwwd I am not getting expected output from specified code in "How to execute.." question. This is not duplicate question. – Rita Mar 20 '19 at 11:39
  • @vikscool I have checked the link you shared. But its different issue. Thanks. – Rita Mar 20 '19 at 11:46
  • @Rita Does your `cmd.exe` live in `~/App_Data/TestRoot/cmd.exe`? That's an odd location for it. `o°/` Also, your `cmd` command is missing the `/c` switch. It should be commandname = `cmd.exe` and arguments = `/c "path\\to\\batfile.bat"` (or `/k` if you wish to keep the cmd console open after batch execution completes). – rojo Mar 20 '19 at 13:10
  • @rojo I tried with updated values of FileName and Arguments as you mentioned, but no luck. Thanks. `processStartInfo.FileName = "cmd.exe"; processStartInfo.Arguments = string.Format("/c \"{0}\"", HttpContext.Current.Server.MapPath(baseDirectory + "/test.bat"));` – Rita Mar 22 '19 at 06:25

1 Answers1

0
 Process process = new Process();
 ProcessStartInfo startInfo = new ProcessStartInfo();
 StartInfo.UseShellExecute = false;
 StartInfo.RedirectStandardOutput = true;
 StartInfo.FileName = "test.bat";
 process.StartInfo = startInfo;
 process.Start();
 string output = process.StandardOutput.ReadToEnd();
 process.WaitForExit();
Mandar Dhadve
  • 371
  • 2
  • 12
  • I tried adding below lines in code, but still no luck. Command prompt is not opening. StartInfo.RedirectStandardOutput = true; string output = process.StandardOutput.ReadToEnd(); – Rita Mar 20 '19 at 11:35
  • If this is running a `.bat` script, perhaps setting `UseShellExecute=true` would help. – lit Mar 20 '19 at 12:08
  • @lit I tried that also, but in this case the WorkingDirectory is different and to set that UseShellExecute must be false. Thanks. – Rita Mar 20 '19 at 12:26