Capture a programs output.

M

Muffin

I am trying to capture the out put of a command line program. Let say ping
or maybe better yet nslookup. I would like to launch and then capture all
the output , redirect it I guess to a string variable or something. I know
how to start it , but not how to capture it. Nslookup I realize can be
started interactively, which is to some degree what I may need to do as I am
discovering that there is no easy way to query DNS svr records with dotnet2.
I have seen some com objects that might but I have not looked to deeply into
this as of yet. I am thinking of using the tools that will be in place where
I plan to use my program, like nslookup, netdiag, dcdiag ...ect. Another
approach that might work, is to embed a cmd window into one of the my
frames, althought I have no idea how to do that or if it will require reams
of code to implement.
Any points, examples, guidence and suggestions would be very welcome.

Thx
 
M

Michael Nemtsev

Hello Muffin,

Use RedirectStandardOutput property of the Process.StartInfo

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

M> I am trying to capture the out put of a command line program. Let say
M> ping
M> or maybe better yet nslookup. I would like to launch and then capture
M> all
M> the output , redirect it I guess to a string variable or something. I
M> know
M> how to start it , but not how to capture it. Nslookup I realize can
M> be
M> started interactively, which is to some degree what I may need to do
M> as I am
M> discovering that there is no easy way to query DNS svr records with
M> dotnet2.
M> I have seen some com objects that might but I have not looked to
M> deeply into
M> this as of yet. I am thinking of using the tools that will be in
M> place where
M> I plan to use my program, like nslookup, netdiag, dcdiag ...ect.
M> Another
M> approach that might work, is to embed a cmd window into one of the my
M> frames, althought I have no idea how to do that or if it will require
M> reams
M> of code to implement.
M> Any points, examples, guidence and suggestions would be very welcome.
M> Thx
M>
 
J

John Vottero

Muffin said:
I am trying to capture the out put of a command line program. Let say ping
or maybe better yet nslookup. I would like to launch and then capture all
the output , redirect it I guess to a string variable or something. I know
how to start it , but not how to capture it. Nslookup I realize can be
started interactively, which is to some degree what I may need to do as I
am discovering that there is no easy way to query DNS svr records with
dotnet2. I have seen some com objects that might but I have not looked to
deeply into this as of yet. I am thinking of using the tools that will be
in place where I plan to use my program, like nslookup, netdiag, dcdiag
...ect. Another approach that might work, is to embed a cmd window into one
of the my frames, althought I have no idea how to do that or if it will
require reams of code to implement.
Any points, examples, guidence and suggestions would be very welcome.

You should look into using PowerShell. From a PowerShell prompt, you can
do:

PS C:\> $savedoutput = nslookup somedomain.com

and the $savedoutput variable will be a string that contains the output of
nslookup.

If you want to add this to an application, it's a lot easier to call a
PowerShell script than it is to create a process and capture the output.
 
J

John Vottero

John Vottero said:
You should look into using PowerShell. From a PowerShell prompt, you can
do:

PS C:\> $savedoutput = nslookup somedomain.com

and the $savedoutput variable will be a string that contains the output of
nslookup.

If you want to add this to an application, it's a lot easier to call a
PowerShell script than it is to create a process and capture the output.

I started wondering about just how easy it would be to do this from a
program so, I gave it a whirl. Turns out, it's only two lines of code:

RunspaceInvoke ri = new RunspaceInvoke();
Collection<PSObject> results = ri.Invoke("nslookup
www.mvpsi.com");

are the two key lines. Here's a complete program:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Management.Automation;

namespace CaptureCommandOutput
{
class Program
{
static void Main(string[] args)
{
using (RunspaceInvoke ri = new RunspaceInvoke())
{
Collection<PSObject> results = ri.Invoke("nslookup
www.mvpsi.com");

Console.WriteLine("nslookup returned:");
foreach (PSObject po in results)
{
Console.WriteLine(po.BaseObject);
}

Console.WriteLine("Press return to contine");
Console.ReadLine();
}
}
}
}
 
M

Miroslav Stampar [MCSD.NET / Security+]

Process scanProcess = new Process();

scanProcess.StartInfo.RedirectStandardError = true;
scanProcess.StartInfo.RedirectStandardOutput = true;
scanProcess.StartInfo.UseShellExecute = false;
scanProcess.StartInfo.FileName = "cmd.exe";
scanProcess.StartInfo.Arguments = "/c nslookup www.google.com";
scanProcess.StartInfo.CreateNoWindow = true;
scanProcess.Start();

StreamReader sOut = scanProcess.StandardOutput;
StringBuilder result = new StringBuilder();
string temp;

while ((temp = sOut.ReadLine()) != null)
result.AppendLine(temp);

sOut = scanProcess.StandardError;

while ((temp = sOut.ReadLine()) != null)
result.AppendLine(temp);

sOut.Close();
scanProcess.Close();

HTH :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top