如何捕获外部程序的错误消息文本 [英] How do catch the text of error messages of external program
本文介绍了如何捕获外部程序的错误消息文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用System.Diagnostics Process运行外部程序。如果出现错误,程序会使用消息框报告此问题,如何捕获错误消息的文本?一些例子?
I'm running an external program using System.Diagnostics Process. In case of errors the program reports this with a messagebox, How can I catch the text of error messages? Some example ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
static void Main()
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "C:/Program Files (x86)/1Cv77/BIN/1cv7s.exe";
proc.StartInfo.Arguments = "enterprise /D D:/1C_Bases/ANRBase_60 /Nemail /Pemail";
proc.EnableRaisingEvents = true;
proc.Start();
proc.WaitForExit();
}
}
}
推荐答案
一般来说,运行任何外部设备都不是最佳选择程序。它意味着单独的过程;并且流程很好地隔离(在单独的地址空间中执行等)如果某个流程不是为某些协作而设计的,那么尝试从中获取任何内容都非常非生产。
但是,某些应用程序可能适合此类协作。这一切都取决于你可以假设的行为。它可以是使用标准控制台的过程,也可以是其他应用程序重定向其输出(以及更少见的输入)的方式。如果是这种情况,您应该使用System.Diagnostic.Process
重定向StandardOutput
和StandardError
。该过程可以在这些流中的任何一个中输出错误信息;你需要检查一下。请参阅:
http://msdn.microsoft.com /en-us/library/vstudio/system.diagnostics.process [ ^ ],
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.standarderror [ ^ ],
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process .standardoutput [ ^ ]。
您可以在上面引用的最后一个MSDN页面上找到一个很好的代码示例。
-SA
Generally, it's not a best idea to run any external program at all. It means the separate process; and the processes are well isolated (executed in separate address spaces, etc.) If a process is not designed for some collaboration, trying to get anything from it is highly non-productive.
However, some applications can be suited for such collaboration. It all depends what behavior you can assume. It can be a process using standard console the way it can be used by other applications redirecting its output (and, more rarely, input). If this is the case, you should useSystem.Diagnostic.Process
redirectingStandardOutput
andStandardError
. The process may output error information in either of those streams; you will need to check it up. Please see:
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.standarderror[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.standardoutput[^].
You will find a good code sample on the last MSDN page referenced above.
—SA
我想你必须尝试显示InnerException消息
i think you have to try to display the InnerException Message
try
{
//Do something
}
catch (Exception e)
{
console.writeLine( e.InnerException.Message )
}
这篇关于如何捕获外部程序的错误消息文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文