C# - 捕获Windows应用程序输出 [英] C# - Capturing Windows Application Output

查看:311
本文介绍了C# - 捕获Windows应用程序输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序启动C:\Windows\System32\Msra.Exe来控制domaincomputer。有没有一种方法,我可以抓住这个msra.Exe显示错误消息。 (从msra.exe即内部错误消息,而不是那些从我的应用程序。)
软件本身是一个Windows窗体应用程序。



任何帮助赞赏。



代码开始MSRA低于...它仅仅是一个完整的应用程序的代码片段。

 字符串MSRA =C:\\Windows\\System32\\runas.exe 

串域名=**********;
串网域控制器=*************;

如果(File.Exists(MSRA)==假)
{
System.Windows.Forms.MessageBox.Show(Runas.exe不found.\\\
\\ \\rPlease请联系您的内部IT的支持,致命错误,System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error)。
}
,否则
{

{
进程p =新工艺();
p.StartInfo.UseShellExecute = TRUE;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.ErrorDialog = TRUE;
p.StartInfo.FileName =微软亚洲研究院;
p.StartInfo.Arguments =/ NOPROFILE / netonly /用户:+域名+\\+用户名+\CMD /服务器:+网域控制器+/ C msra.exe / offerra+ computerip +\;
p.Start();
p.Dispose();
Thread.sleep代码(1700);
SendKeys.SendWait(密码);
SendKeys.SendWait({} ENTER);
}

{
System.Windows.Forms.MessageBox.Show(MSRA无法启动,原因未知);
}
}


解决方案

您可以设置 RedirectStandardOutput RedirectStandardError 真正要能从过程的标准输出或错误输出读



然后,您有几种选择如何实际读取数据:




  • 使用 StandardOutput 属性

  • subsribe到 OutputDataReceived 事件并调用 BeginOutputReadLine()



或相应的成员的错误流。


My application launches "C:\Windows\System32\Msra.Exe" to control a domaincomputer. Is there a way I can capture the error messages that this msra.Exe shows. (I.e. internal error messages from the msra.exe and not the ones from my app.) The app itself is a windows Forms app.

Any help is appreciated.

The Code to start MSRA is below... it is just a snippet of the complete application.

string msra = "C:\\Windows\\System32\\runas.exe";

string domainname = "**********";
string domaincontroller = "*************";

if (File.Exists(msra) == false)
{
    System.Windows.Forms.MessageBox.Show("Runas.exe not found.\n\rPlease contact your internal IT support.", "Fatal Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else
{
    try
    {
        Process p = new Process();
        p.StartInfo.UseShellExecute = true;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        p.StartInfo.ErrorDialog = true;
        p.StartInfo.FileName = msra;
        p.StartInfo.Arguments = "/noprofile /netonly /user:" + domainname + "\\" + username + " \"cmd /server:" + domaincontroller + " /C msra.exe /offerra " + computerip + "\"";
        p.Start();
        p.Dispose();
        Thread.Sleep(1700);
        SendKeys.SendWait(password);
        SendKeys.SendWait("{ENTER}");
    }
    catch
    {
        System.Windows.Forms.MessageBox.Show("MSRA could not be started for an unknown reason");
    }
}

解决方案

You can set RedirectStandardOutput or RedirectStandardError to true to be able to read from the standard output or error output of the process.

You then have several options how to actually read the data:

  • use StandardOutput property
  • subsribe to the OutputDataReceived event and call BeginOutputReadLine()

Or the corresponding members for the error stream.

这篇关于C# - 捕获Windows应用程序输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆