1

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");
    }
}
XikiryoX
  • 1,898
  • 1
  • 12
  • 33
  • possible duplicate of [Redirect console output to textbox in separate program C#](http://stackoverflow.com/questions/415620/redirect-console-output-to-textbox-in-separate-program-c-sharp) – tenfour Oct 24 '11 at 15:05

3 Answers3

2

You need to http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

[Updated to point to a .net example]

Ross Dargan
  • 5,876
  • 4
  • 40
  • 53
2

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.

svick
  • 236,525
  • 50
  • 385
  • 514
1

You are using Process so try the Process.StandardError property. You assign it a stream and you will be able to use it.

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx

And while you're there you can also use Process.StandardOutput

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

Khan
  • 516
  • 11
  • 24