Silahkan Melihat Tutorial di website kami dengan nyaman ENJOY YOUR LIFE ☕

VB.NET :: Launching and Controlling External Applications from VB.NET Application


Today we will see how to launch external applications from within VB.NET application and control its input and output. For this demo, we will make our own Command Prompt – a form from where you can execute DOS commands and get the result too.

The Problem

How to control an external program from within our VB.NET application, send input to it and get output from it.

The Solution

Start a new application (or add a form to your application).
Add two textboxes and a button to it.
Set the following properties:
TextBox1
Name       = OutputTextBoxMultiline  = TrueReadOnly   = TrueScrollbars = Both
TextBox2
Name       = InputTextBoxTabIndex   = 0
Button1
Name       = ExecuteButtonText       = Execute
Arrange the controls on the form appropriately. See screenshot below:
Open the code window and add the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Private WithEvents MyProcess As Process
Private Delegate Sub AppendOutputTextDelegate(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.Start()
 
    MyProcess.BeginErrorReadLine()
    MyProcess.BeginOutputReadLine()
    AppendOutputText("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
    MyProcess.StandardInput.Flush()
    MyProcess.Close()
End Sub
 
Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived
    AppendOutputText(vbCrLf & "Error: " & e.Data)
End Sub
 
Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived
    AppendOutputText(vbCrLf & e.Data)
End Sub
 
Private Sub ExecuteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteButton.Click
    MyProcess.StandardInput.WriteLine(InputTextBox.Text)
    MyProcess.StandardInput.Flush()
    InputTextBox.Text = ""
End Sub
 
Private Sub AppendOutputText(ByVal text As String)
    If OutputTextBox.InvokeRequired Then
        Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText)
        Me.Invoke(myDelegate, text)
    Else
        OutputTextBox.AppendText(text)
    End If
End Sub
Run the code. Enter any DOS command in the InputTextBox and press enter (or click Execute button). The output should appear in the OutputTextBox.
If you are not familiar with DOS, type HELP and it should show you the list of all commands it recognizes.
Enjoy!




0 komentar:

Post a Comment

VB.NET :: Launching and Controlling External Applications from VB.NET Application