问题
I am running an exe file from C# code, due to some reasons error occurs in that exe file. It keeps waiting and ultimately gives a popup error "abc.exe has stopped working" and so on, but it does not exit.
//The below code is calling it successfully
public bool callExe(string exePath)
{
Log("EXE excution Started : " + exePath, INFO_TAG);
try
{
Process exeProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = exePath;
exeProcess.StartInfo = startInfo;
startInfo.Arguments = this.prjId.ToString();
exeProcess.Start();
exeProcess.WaitForExit();
int exitCode = exeProcess.ExitCode;
return true;
}
catch (Exception e)
{
errorCode = EXE_ERROR;
return false;
}
}
Though I can capture exit code, but I can do so only if it exits.
回答1:
This link might be useful https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandarderror%28v=vs.110%29.aspx
Subscribe to ErrorDataReceived event and get the error message. enter link description here
Instead of waiting for it to exit, you can subscribe to Process Exit event
If your exe is giving some kind of unhandled exception, you won't be able to get the exact error. Windows event viewer may help you in getting the error number and possible causes.
来源:https://stackoverflow.com/questions/31938099/catch-error-in-exe-file-if-called-from-c-sharp-code