Note that there are some explanatory texts on larger screens.

plurals
  1. POObtain Exitcode from CMD using c#
    text
    copied!<p>I am using following code to write the PATH, EXECUTABLE NAME and ARGUMENTS to a batch file and execute it through CMD using c#. The problem is sometimes the application dosent starts up after executing the batch file. And the c# code dosent give me exception or any notification.</p> <p>For which i want to get the Exitcode from CMD to determine if the commands executed properly. How can i determine Exit code ? </p> <pre><code> public void Execute() { try { string LatestFileName = GetLastWrittenBatchFile(); if (System.IO.File.Exists(BatchPath + LatestFileName)) { System.Diagnostics.ProcessStartInfo procinfo = new System.Diagnostics.ProcessStartInfo("cmd.exe"); procinfo.UseShellExecute = false; procinfo.RedirectStandardError = true; procinfo.RedirectStandardInput = true; procinfo.RedirectStandardOutput = true; System.Diagnostics.Process process = System.Diagnostics.Process.Start(procinfo); System.IO.StreamReader stream = System.IO.File.OpenText(BatchPath + LatestFileName); System.IO.StreamReader sroutput = process.StandardOutput; System.IO.StreamWriter srinput = process.StandardInput; while (stream.Peek() != -1) { srinput.WriteLine(stream.ReadLine()); } Log.Flow_writeToLogFile("Executed .Bat file : " + LatestFileName); process.WaitForExit(1000); if (process.ExitCode != 0) { int iExitCode = process.ExitCode; } stream.Close(); process.Close(); srinput.Close(); sroutput.Close(); } else { ExceptionHandler.writeToLogFile("File not found"); } } catch (Exception ex) { ExceptionHandler.writeToLogFile(System.Environment.NewLine + "Target : " + ex.TargetSite.ToString() + System.Environment.NewLine + "Message : " + ex.Message.ToString() + System.Environment.NewLine + "Stack : " + ex.StackTrace.ToString()); } } </code></pre> <p>_________________Update___________________</p> <p>script inside Batchfile : [Note that Notepads.exe is wrong to get the error ]</p> <p>START Notepads.EXE</p> <p>"if "%ERRORLEVEL%" == "1" exit /B 1"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload