473,497 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read Command Line Output

How can I read the output from a command line utility such as ping,
nslookup, etc, from within my app??? I know that it is possible to
output the results to a text file using the > function in a command
window and then read the file, but doing this for 1000+ files will
cause a lot of unnecessary read/writes to the disc and affect
performance...

I am not asking for a .NET way to ping or do a DNS lookup, I am
interested in the concept of reading the output from a command line
utility...

Thanks
Nov 20 '05 #1
4 1627
In article <7a**************************@posting.google.com >, Steve wrote:
How can I read the output from a command line utility such as ping,
nslookup, etc, from within my app??? I know that it is possible to
output the results to a text file using the > function in a command
window and then read the file, but doing this for 1000+ files will
cause a lot of unnecessary read/writes to the disc and affect
performance...

I am not asking for a .NET way to ping or do a DNS lookup, I am
interested in the concept of reading the output from a command line
utility...

Thanks


You use System.Diagnostics.Process to start the application, and then
you redirect it's standard output by making sure the startinfo objects'
useshellexecute is set to false and the redirectstandardoutput is set to
true. Then you can use the processes StandardOutput property to read
the text....

--
Tom Shelton [MVP]
Nov 20 '05 #2
Hi Steve,

Roughly copied from a program and changed

I hope this helps?

Cor
\\\
Dim p As New Process
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.Arguments = myargumentstring
p.StartInfo.WorkingDirectory = myworkdirectorystring
p.StartInfo.FileName =C:\myprogram
p.Start()
Dim sr As IO.StreamReader = p.StandardOutput
Dim sb As New System.Text.StringBuilder("")
Dim input As Integer = sr.Read
Do Until input = -1
sb.Append(ChrW(input))
input = sr.Read
Loop
///
Nov 20 '05 #3
* go****@valleyboy.net (Steve) scripsit:
How can I read the output from a command line utility such as ping,
nslookup, etc, from within my app??? I know that it is possible to
output the results to a text file using the > function in a command
window and then read the file, but doing this for 1000+ files will
cause a lot of unnecessary read/writes to the disc and affect
performance...


<URL:http://dotnet.mvps.org/dotnet/samples/miscsamples/downloads/RedirectConsole.zip>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
Perfect, thanks... I have created a function if anyone is interested
(watch for line breaks)...

Friend Function ReadCmdOutput(ByVal applicationName As String,
Optional ByVal applicationArgs As String = "", Optional ByVal
workingDirectory As String = "", Optional ByVal showWindow As Boolean
= False) As String
Try
Dim processObj As New Process

processObj.StartInfo.UseShellExecute = False
processObj.StartInfo.RedirectStandardOutput = True
processObj.StartInfo.FileName = applicationName
processObj.StartInfo.Arguments = applicationArgs
processObj.StartInfo.WorkingDirectory = workingDirectory

If showWindow = True Then
processObj.StartInfo.CreateNoWindow = False
Else
processObj.StartInfo.CreateNoWindow = True
End If

processObj.Start()
processObj.WaitForExit()

Return processObj.StandardOutput.ReadToEnd
Catch ex As Exception
Return ""
End Try
End Function
"Cor Ligthert" <no**********@planet.nl> wrote in message news:<eR**************@tk2msftngp13.phx.gbl>...
Hi Steve,

Roughly copied from a program and changed

I hope this helps?

Cor
\\\
Dim p As New Process
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.Arguments = myargumentstring
p.StartInfo.WorkingDirectory = myworkdirectorystring
p.StartInfo.FileName =C:\myprogram
p.Start()
Dim sr As IO.StreamReader = p.StandardOutput
Dim sb As New System.Text.StringBuilder("")
Dim input As Integer = sr.Read
Do Until input = -1
sb.Append(ChrW(input))
input = sr.Read
Loop
///

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4406
by: Steve | last post by:
How can I read the output from a command line utility such as ping, nslookup, etc, from within my app??? I know that it is possible to output the results to a text file using the > function in a...
2
22743
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
18
4852
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
4
5798
by: Martin Hvidberg | last post by:
Dear group I need to make a very simple piece of code in C, that can be command line executed and will compile on Linux, i.e. gcc. It should read a ascii Comma Separated Values (CSV) file and...
3
2736
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
0
1222
by: Alison | last post by:
Hi, all, I started an application by using process and wanted to redirect the output to a textbox in VB .Net. I used newprocess.standoutput.readline since I wanted the display to be timely . The...
2
2100
by: sani8888 | last post by:
Hi everybody I am a beginner with C++ programming. And I need some help. How can I start with this program *********** The program is using a text file of information as the source of the...
1
2434
by: NeoGregorian | last post by:
Hello, I am writing a wrapper to a basic Input/Output programs (where you type a one line command at a time and then get 0 or more lines of output before you can input the next command). I'm...
6
5682
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
2729
by: tvaughan77 | last post by:
Hi, I have some code that I want to use to run a command line utility and I want to be able to run it from an aspx page running under IIS. The command line utility is a local utility running on...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
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,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
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...
0
5456
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,...
0
4584
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...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.