Get the output of a shell Command in VB.net Get the output of a shell Command in VB.net windows windows

Get the output of a shell Command in VB.net


You won't be able to capture the output from Shell.

You will need to change this to a process and you will need to capture the the Standard Output (and possibly Error) streams from the process.

Here is an example:

        Dim oProcess As New Process()        Dim oStartInfo As New ProcessStartInfo("ApplicationName.exe", "arguments")        oStartInfo.UseShellExecute = False        oStartInfo.RedirectStandardOutput = True        oProcess.StartInfo = oStartInfo        oProcess.Start()        Dim sOutput As String        Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput            sOutput = oStreamReader.ReadToEnd()        End Using        Console.WriteLine(sOutput)

To get the standard error:

'Add this next to standard output redirect oStartInfo.RedirectStandardError = True'Add this belowUsing oStreamReader As System.IO.StreamReader = checkOut.StandardError        sOutput = oStreamReader.ReadToEnd()End Using


Just pipe the output to a text file?

MyCommand > "c:\file.txt"

Then read the file.


    Dim proc As New Process    proc.StartInfo.FileName = "C:\ipconfig.bat"       proc.StartInfo.UseShellExecute = False    proc.StartInfo.RedirectStandardOutput = True    proc.Start()    proc.WaitForExit()    Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))    For Each ln As String In output        RichTextBox1.AppendText(ln & vbNewLine)        lstScan.Items.Add(ln & vbNewLine)    Next

=======================================================================create a batch file in two lines as shown below:

    echo off    ipconfig

' make sure you save this batch file as ipconfig.bat or whatever name u decide to pick but make sure u put dot bat at the end of it.