שתף באמצעות


Need to capture shell output into a textbox

Question

Monday, October 14, 2013 10:02 PM

Hi ! I need to capture console output, when writing command Shell("ipconfig"). I need the output in my richtextbox. Any ideas? Ive searched whole internet, there were codes and helps, but not that I want. Like you open cmd, write ipconfig, so you get "answer". I need the "answer" in my textbox. I hope you understand me, im not from english country. 

All replies (3)

Tuesday, October 15, 2013 3:21 AM ✅Answered | 1 vote

Hi,

 As Armin has said, you may be able to get the info you want using the System.Net.NetworkInformation namespace without going threw the hassle of reading and parsing the text returned from the command window to get just the part you want. However, if you still want to read the output from the command window then here is a one way of using a Process and redirecting the output. You can test it by creating a new Form project and adding 1 Button and 1 RichTextBox to it. Then use the code below.

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.WordWrap = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim proc As New Process
        proc.StartInfo.FileName = "cmd.exe"
        proc.StartInfo.Arguments = "/k ipconfig"
        proc.StartInfo.CreateNoWindow = True
        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)
        Next
    End Sub
End Class

Here is what it shows. You may want to parse it to get only what info you want from the (output) string array.


Monday, October 14, 2013 10:14 PM | 2 votes

Hi,

replace Shell by Process.Start: http://msdn.microsoft.com/en-us/library/System.Diagnostics.Process.Start

Then you can perform redirection of standard input/output. Look at the example here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

Note that the Framework offers network information via System.Net.NetworkInformation: http://msdn.microsoft.com/en-us/library/System.Net.NetworkInformation. So maybe you don't need to start ipconfig.

Armin


Friday, July 14, 2017 6:35 AM

I know it's been 4 years ago since this post is up but since i have this at hand and i figure someone else can get some use out of it if u don't :D

        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

===============================================================

created file with whatever name u want like ipconfig.bat

echo off

ipconfig

and save it as bat file