473,837 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7175
Hello Muffin,

Use RedirectStandar dOutput property of the Process.StartIn fo

---
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******** *************** ***********@mic rosoft.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<PSOb jectresults = ri.Invoke("nslo okup
www.mvpsi.com") ;

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

using System;
using System.Collecti ons.Generic;
using System.Collecti ons.ObjectModel ;
using System.Text;
using System.Manageme nt.Automation;

namespace CaptureCommandO utput
{
class Program
{
static void Main(string[] args)
{
using (RunspaceInvoke ri = new RunspaceInvoke( ))
{
Collection<PSOb jectresults = ri.Invoke("nslo okup
www.mvpsi.com") ;

Console.WriteLi ne("nslookup returned:");
foreach (PSObject po in results)
{
Console.WriteLi ne(po.BaseObjec t);
}

Console.WriteLi ne("Press return to contine");
Console.ReadLin e();
}
}
}
}
May 26 '07 #4
Process scanProcess = new Process();

scanProcess.Sta rtInfo.Redirect StandardError = true;
scanProcess.Sta rtInfo.Redirect StandardOutput = true;
scanProcess.Sta rtInfo.UseShell Execute = false;
scanProcess.Sta rtInfo.FileName = "cmd.exe";
scanProcess.Sta rtInfo.Argument s = "/c nslookup www.google.com" ;
scanProcess.Sta rtInfo.CreateNo Window = true;
scanProcess.Sta rt();

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

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

sOut = scanProcess.Sta ndardError;

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

sOut.Close();
scanProcess.Clo se();

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4648
by: Frank Teoh | last post by:
I am trying to capture the output of os.system() into a string without success. Since os.system() returns only its exit_status, how can I do that? system(...) system(command) -> exit_status Execute the command (a string) in a subshell. My aim is to capture the return value of os.system("rsh <netapp_filer_host> qtree")
9
2725
by: Jay Donnell | last post by:
I have a function that is a few thousand lines of code (I didn't write it) and I want to capture the output and save it as a variable. This is very easy to do in php. It would look something like this. <?php function myFunc(){ echo "something else" }
0
2710
by: Tsunami | last post by:
Hi to all: I have a big problem and I write to ask for help. Sorry if this message does not arrive to the apropriate group. I have selected them paying attention but I am not sure to do it well. I want to capture a fotogram of a media player control in VB6 and I capture the screen as in the section "Visual Basic 4.0 16/32 Capture Routines" of this URL:
0
1385
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any file he wants - are you trying to trap any file operation it may want to do? You *could* do such things -google for "code injection" and "API hooking"- but I doubt it's what you really want.
1
3711
Atran
by: Atran | last post by:
Hello, When a key pressed on keyboard, I want my program capture the pressed key, and save the pressed keys in a notepad. You listen about monitor programs, that programs capture keys........ How I can capture a pressed key? If it possible, How to capture the screen (Screen picture)? Thanks for anyhelp.
10
17713
by: lalnamar | last post by:
I want to execute a unix time command using perl, and capture the output (specifically, the time it takes the command to execute). i tried using the system command: system("time command "); however, when i try to capture the output, the capture output does not contain the amount of time it took to execute when i try to do: $var = `time command `; there is the same problem (the ouput from the command is saved in the variable; however,...
2
3104
by: jdbartlett | last post by:
I'm trying to capture output from a command line utility (XMLSec), but only get an empty result. If I call the script from the command line, I see XMLSec's output, but I can't seem to capture it! My PHP installation is working correctly and captures other command line output just fine, XMLSec is the only exception I've found. I've also tried a couple of other systems to confirm this behavior. Capture methods I've tried include:
6
7991
by: k.vanderstarren | last post by:
Hi Everyone, Do any of you gurus out there have any suggestions regarding capturing audio/video from a video capture card in C#. Although I've written other programs, I've never worked with audio/video sources before. I've seen other people recommending DirectShow.NET. Is this one of the more popular libraries to use? Would anyone recommend other third- party libraries? Thanks,
13
1974
by: Jim Langston | last post by:
I had asked this in comp.lang.c++ with out any answers that would actually work, so I'm hoping someone here may know a way. I am calling C library functions that want to output to stdout. I need to capture this to memory for use internally inside the program without modifying the library calls that actually do the output. This is for the GraphViz libraries and it is the output of dot that I'm actually trying to capture. I have...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10864
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10615
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9390
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6989
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5842
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4469
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4036
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.