PAEXEC can't capture stderr in C#

Viewed 0

After tried calling paexec from C# program, it can't capture any stderr, tested my program on psexec, it can capture stderr, however for paexec, if runs from a command shell, it can see the stderr, but can't see it appeared on my program. Is this fault on paexec?

void Main() { var cmdargs =@"machine c:windowssystem32inetsrvappcmd list apppool"; for(int i=0;i<10;i++) { Debug.WriteLine("Trial #{0}",i); psexec(cmdargs, true); } }

// ISSUE: if paexec used, nothing from stderr is captured at all (stdout seems ok) // (with psexec, stderr is captured, but due to other reasons psexec fails) string pexec() {return "paexec.exe";} // Define other methods and classes here

void psexec(string cmdargs, bool showOutput=true) { // uses method described in http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx // Read at least one stream asynchronously to avoid deadlock condition StringBuilder outputTxt=new StringBuilder(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName =pexec(); startInfo.Arguments = cmdargs; Debug.WriteLine(cmdargs); startInfo.UseShellExecute = false; startInfo.RedirectStandardError = true; startInfo.RedirectStandardOutput = true; startInfo.CreateNoWindow = true; Process process = new Process(); process.StartInfo = startInfo; process.OutputDataReceived += new DataReceivedEventHandler( (object proc,DataReceivedEventArgs outline)=>{ if (!String.IsNullOrEmpty(outline.Data)) outputTxt.Append(Environment.NewLine+outline.Data); }); process.Start(); process.BeginOutputReadLine(); string errTxt=process.StandardError.ReadToEnd();

    process.WaitForExit();
    Debug.WriteLine("Process exit code: {0}",process.ExitCode);
    process.Close();
    if (showOutput){
        Debug.WriteLine("ERR:"+errTxt);
                       Debug.WriteLine("OUT:"+outputTxt);
    }

}

0 Answers
Related