2011-10-24 69 views
0

我的应用程序启动“C:\ Windows \ System32 \ Msra.Exe”来控制域计算机。有没有一种方法可以捕获此msra.Exe显示的错误消息。 (即来自msra.exe的内部错误消息,而不是来自我的应用程序的内部错误消息)。 该应用程序本身是一个Windows窗体应用程序。C# - 捕获Windows应用程序输出

任何帮助表示赞赏。

启动MSRA的代码位于下方...它只是完整应用程序的一个片段。

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"); 
    } 
} 
+0

可能重复[控制台输出重定向到单独的程序C#文本框(HTTP://计算器.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c-sharp) – tenfour

回答

1

可以设置RedirectStandardOutputRedirectStandardErrortrue能够从标准输出或错误输出到读该过程。

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

  • 使用StandardOutput财产
  • subsribe到OutputDataReceived事件并调用BeginOutputReadLine()

或为错误流的相应成员。

+0

谢谢 - 这确实是继续下去的方法。 – XikiryoX

相关问题