简体   繁体   中英

Write a visual basic script in a vb.net application.

In my application I have created I need it to write a visual basic script to change the ip address

The script is as follows...

Dim strIPAddress
Dim strSubnetMask
Dim strGateway
Dim intGatewayMetric
Dim strDns1
Dim strDns2

strIPAddress = "192.168.1.211"
strSubnetMask = "255.255.255.0"
strGateway = "192.168.0.11"
intGatewayMetric = 1
strDns1 = "8.8.8.8"
strDns2 = "4.4.4.4"

Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
objShell.Run "netsh interface ip set dns name=""Local Area Connection"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit

The way I am trying to write this is like the following..

    Directory.CreateDirectory("C:\SpecMee\IPChanger\")
    Dim objwriter As New System.IO.StreamWriter("C:\SpecMee\IPChanger\IpChanger.vbs")


    objwriter.WriteLine("Dim strIPAddress" & Environment.NewLine & "Dim strSubnetMask" & Environment.NewLine & "Dim strGateWay" & Environment.NewLine & "Dim intGatewayMetric" & Environment.NewLine & "Dim strDns1" & Environment.NewLine & "Dim strDns2" & Environment.NewLine & "strIPAddress = """ & Environment.NewLine & TB_IPAddress & Environment.NewLine &)

    objwriter.Close()
    MessageBox.Show("Created")

The problem I am having is one, it is taking ages and two, how would i include the "" in the script.

I dont mind spending time on this, I just dont know if im going about this the right way.

any help would be appreciated.

Thanks Chris

Add the script as a text file resource to your project. Replace all instances of the IP Address in the script with some known value, then use string.replace:

Const ipPlaceHolder As String = "##IP##"
    Dim scriptContent As String = My.Resources.script.Replace(ipPlaceHolder, myTextBox.Text)
    IO.File.WriteAllText("C:\SpecMee\IPChanger\IpChanger.vbs", scriptContent)

Where myTextBox contains user input and the text file resource is named "Script"

Two quotation marks in a row makes one in the output.
"And he said ""Thar she blows!"""

( You have two questions in one. That is not considered good customs on Stackoverflow. )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM