Joep
Joep

Reputation: 39

passing vb parameters to batch file

I am trying to carry out next batch file:

route ADD 131.1.0.0 MASK 255.255.0.0 191.168.0.136
pause

However, I would like to make the ip addresses variable by a visual basic app.

Now I tried next:

Public Class Form1
    Dim a

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        a = (TextBox1.Text)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim objWshShell
        objWshShell = CreateObject("Wscript.Shell")
        objWshShell.run(Chr(34) & "C:\NAT\RouteHCM.bat" & Chr(34) & a, 1)

    End Sub
End Class

How can I add more variables and is it possible to make this batch script ( add route in visual basic directly) ?

Upvotes: 0

Views: 2960

Answers (1)

miroxlav
miroxlav

Reputation: 12204

This way:

  • assume you have TextBox1, TextBox2, TextBox3 on the form

  • replace the entire class with the following:

Imports System.Diagnostics

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        With New Process()
            .StartInfo.UseShellExecute = False
            .StartInfo.FileName = "route"
            .StartInfo.Arguments = String.Format("-p add {0} mask {1} {2}",
                                                 TextBox1.Text,
                                                 TextBox2.Text,
                                                 TextBox3.Text)
            .StartInfo.RedirectStandardOutput = True
            .StartInfo.StandardOutputEncoding = Text.Encoding.ASCII
            .Start()
        End With
    End Sub
End Class

If you check help for String.Format(), Process or StartInfo, you will get the idea. (I'm learning things from there, too.)

Upvotes: 1

Related Questions