XikiryoX
XikiryoX

Reputation: 1928

C# - Capturing Windows Application Output

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

Upvotes: 1

Views: 1593

Answers (3)

Ross Dargan
Ross Dargan

Reputation: 6021

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

[Updated to point to a .net example]

Upvotes: 2

Khan
Khan

Reputation: 516

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

Upvotes: 1

svick
svick

Reputation: 244767

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.

Upvotes: 2

Related Questions