C# Powershell host, capturing output of launched console apps
I've written a custom powershell host that pr开发者_JS百科ovides a winforms-based interactive shell. The examples MSDN were excellent and I was able to get something working in a few hours.
All of the standard built-in commands like dir, etc work as expected. However, when I try to launch another console app,like the FTP client, that app starts sending output to stdout. I can see this by changing the application type in VS from windows to console. As soon as I launch FTP from my interactive shell, FTP starts talking to the console window.
Anyone tackled this before? In C++ I could just redirect stdin and stdout and be done with it, but I don't see a way to do this in c#. Help!
update
Okay, anything I run that is not a built-in command immediately starts interacting with the console window.
Check this out, maybe it can help
Process p = new Process();
p.StartInfo.FileName = "netstat";
p.StartInfo.Arguments = "-a";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
System.Diagnostics.Debug.WriteLine(p.StandardOutput.ReadToEnd());
I have finally found a working example here! While it doesn't use the Powershell classes in the .NET framework, it does exactly what I need, including successfully capturing io from launched console apps.
精彩评论