【问题标题】:Catch ERROR in EXE file if called from C# code如果从 C# 代码调用,则在 EXE 文件中捕获错误
【发布时间】:2015-08-11 18:17:25
【问题描述】:

我正在从 C# 代码运行一个 exe 文件,由于某些原因,该 exe 文件中出现错误。它一直在等待,最终弹出错误“abc.exe 已停止工作”等,但它并没有退出。

 //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;
                }
            }

虽然我可以捕获退出代码,但只有在它退出时才能这样做。

【问题讨论】:

  • 命令行exe还是有GUI?
  • @Sarvesh 没有 GUI,只有一个 exe 文件
  • 异常被隔离到它们自己的进程中。除非您想编写半个调试器以便加载另一个 exe,安装(非托管)未处理的异常处理程序然后运行该 exe,否则您将无法捕获另一个进程中抛出的异常。
  • 您可能想要查看该系统上的事件日志 - has stopped working 消息有时会伴随有一个包含更多信息的条目。

标签: c# exe


【解决方案1】:

这个链接可能有用https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandarderror%28v=vs.110%29.aspx

订阅 ErrorDataReceived 事件并获取错误消息。 enter link description here

您可以订阅进程退出事件,而不是等待它退出

如果您的 exe 给出某种未处理的异常,您将无法获得确切的错误。 Windows 事件查看器可能会帮助您获取错误编号和可能的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    相关资源
    最近更新 更多