all groups > dotnet clr > april 2006 >
You're in the

dotnet clr

group:

Is it possible to run a console function and capture the result.


Is it possible to run a console function and capture the result. Ken
4/8/2006 3:25:01 PM
dotnet clr:
I and not sure if this is the correct forum to post this queston. Any ideas
will be appreciated.

Need to call a base windows console command function (like dir or chkdsk)
and capture the results for further processing.

Two questions:
(1) Is it possible to code a program in C# to do this? - I know everyone
says I should use Perl instead.
Re: Is it possible to run a console function and capture the result. Jon Shemitz
4/8/2006 11:03:20 PM
[quoted text, click to view]

Yes - you use the System.Diagnostics.Process class. (For a shell
command like dir, I think you'll have to run command.exe, and pass it
a "dir" argument - I haven't tried this.)

[quoted text, click to view]

Yes.

Here's an example that I wrote (two days ago, fwiw) to capture PING
output:

private static string PingServer()
{
Process Ping = new Process();
Ping.StartInfo.UseShellExecute = false;
Ping.StartInfo.FileName = "ping.exe";
Ping.StartInfo.Arguments = @"-n 1 -r 9 www.midnightbeach.com";
Ping.StartInfo.CreateNoWindow = true;
Ping.StartInfo.RedirectStandardOutput = true;
Ping.Start();
return Ping.StandardOutput.ReadToEnd();
}

--

<http://www.midnightbeach.com> Contracting, consulting, training
..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>
AddThis Social Bookmark Button