Below are my current codes that will run a .VBS file.

When I run the code it will open an CMD app to show the process.

Now how can I run the .VBS file and the process will be shown in Textbox/Richtextbox.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim t As String = Cmb1.SelectedItem

        If Cmb1.SelectedItem = "" Or Cmb2.SelectedItem = "" Then
            MsgBox("Error", MsgBoxStyle.Critical, Me.Text)


                Dim proc As Process = Nothing
                Dim batDir As String = String.Format("C:\")
                proc = New Process()
                proc.StartInfo.WorkingDirectory = batDir
                proc.StartInfo.FileName = t.Trim + ".vbs"
                proc.StartInfo.CreateNoWindow = False

            Catch ex As Exception

            End Try

        End If

    End Sub

The trick is to redirect the standard output.

Have a look at the attached VB 2010 project. It implements a GUI where you can type a DOS command into a textbox and execute it. It runs the command in a separate process and the output is captured and displayed in another textbox. You could adapt this to execute a vbs file or any other DOS program.

'  Name:
'    CommandShell.vb
'  Description:
'    GUI front end for a command line shell 
'  Notes:
'    Type all the regular DOS commands in the text box input and they are
'    executed with the results displayed in the top portion of the display.
'  Audit:
'    2012-11-01  rj  sample code

Public Class Form1

    'Note - because the spawned process is running in a different thread, the only way
    'for it to write to a control in this process is to use a delegate.

    Private WithEvents MyProcess As Process

    Private Delegate Sub AppendStdOutDelegate(ByVal text As String)
    Private Delegate Sub AppendStdErrDelegate(ByVal text As String)

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.AcceptButton = ExecuteButton

        MyProcess = New Process

        With MyProcess.StartInfo
            .FileName = "CMD.EXE"
            .UseShellExecute = False
            .CreateNoWindow = True
            .RedirectStandardInput = True
            .RedirectStandardOutput = True
            .RedirectStandardError = True
        End With


        MyProcess.BeginErrorReadLine()      'start async read on stderr
        MyProcess.BeginOutputReadLine()     'start async read on stdout

        'AppendStdOut("Process Started at: " & MyProcess.StartTime.ToString)

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        MyProcess.StandardInput.WriteLine("EXIT") 'send an EXIT command to the Command Prompt

    End Sub

    Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived

        AppendStdErr(vbCrLf & "Error: " & e.Data)

    End Sub

    Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived

        AppendStdOut(vbCrLf & e.Data)

    End Sub

    Private Sub ExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteButton.Click

        InputTextBox.Text = ""

    End Sub

    Private Sub AppendStdOut(ByVal text As String)

        If txtStdOut.InvokeRequired Then
            Dim myDelegate As New AppendStdOutDelegate(AddressOf AppendStdOut)
            Me.Invoke(myDelegate, text)
        End If

    End Sub

    Private Sub AppendStdErr(ByVal text As String)

        If txtStdOut.InvokeRequired Then
            Dim myDelegate As New AppendStdErrDelegate(AddressOf AppendStdErr)
            Me.Invoke(myDelegate, text)
        End If

    End Sub

    Private Sub Submit(cmd As String)


    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.