472,146 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Capture a programs output.

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
May 26 '07 #1
5 7065
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

MI am trying to capture the out put of a command line program. Let say
Mping
Mor maybe better yet nslookup. I would like to launch and then capture
Mall
Mthe output , redirect it I guess to a string variable or something. I
Mknow
Mhow to start it , but not how to capture it. Nslookup I realize can
Mbe
Mstarted interactively, which is to some degree what I may need to do
Mas I am
Mdiscovering that there is no easy way to query DNS svr records with
Mdotnet2.
MI have seen some com objects that might but I have not looked to
Mdeeply into
Mthis as of yet. I am thinking of using the tools that will be in
Mplace where
MI plan to use my program, like nslookup, netdiag, dcdiag ...ect.
MAnother
Mapproach that might work, is to embed a cmd window into one of the my
Mframes, althought I have no idea how to do that or if it will require
Mreams
Mof code to implement.
MAny points, examples, guidence and suggestions would be very welcome.
MThx
M>
May 26 '07 #2
"Muffin" <mu****@NoEmail.localwrote in message
news:PZ******************************@comcast.com. ..
>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.

May 26 '07 #3
"John Vottero" <JV******@mvpsi.comwrote in message
news:FA**********************************@microsof t.com...
"Muffin" <mu****@NoEmail.localwrote in message
news:PZ******************************@comcast.com. ..
>>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.
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<PSObjectresults = 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<PSObjectresults = 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();
}
}
}
}
May 26 '07 #4
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 :)

May 27 '07 #5
Thx for the help everyone. Thats great.

"Muffin" <mu****@NoEmail.localwrote in message
news:PZ******************************@comcast.com. ..
>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

May 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Frank Teoh | last post: by
reply views Thread by Tsunami | last post: by
2 posts views Thread by jdbartlett | last post: by
6 posts views Thread by k.vanderstarren | last post: by
13 posts views Thread by Jim Langston | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.