Run a second console app and read its output.
I came accross an interesting email from Bill (WEB1110) in the microsoft.public.dotnet.languages.csharp newsgroup, its all about console output catch. i have changed/modified the example Bill has provided.
We will create a C# application that uses another application (console) output.
we will fire the ping.exe application with the arg 127.0.0.1 and catch the output to a string.
using System;
using System.Diagnostics;
namespace ConsoleApplication8
{
class Class1
{
static void Main(string[] args)
{
ProcessStartInfo proc = new ProcessStartInfo(); / a process holder */
proc.FileName = "ping.exe";
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.Arguments = "127.0.0.1";
proc.UseShellExecute = false; /*do not show console for the process - a must*/
Process p = Process.Start(proc);
string res;
res = p.StandardOutput.ReadToEnd();
System.Console.WriteLine(res);
p.WaitForExit(); /*wait indefinitely for the associated process to exit*/
}
}
}
this is a neat one!
|
|
© Copyright
2002
Sagiv Hadaya.
Last update:
10/11/2002; 2:16:23 AM. |
|