Catch ERROR in EXE file if called from C# code

我只是一个虾纸丫 提交于 2019-12-13 09:24:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!