Read Command Line Output

S

Steve

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
 
T

Tom Shelton

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....
 
C

Cor Ligthert

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
///
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (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>
 
S

Steve

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
 

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