0

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

3
  • Why do you have to write it programmatically? Commented Jul 10, 2012 at 21:22
  • I need to write it inside the application because I need to allow the user to change the ip addresses inside the script so it is more useful towards it. Commented Jul 10, 2012 at 21:24
  • And steve, thanks for the advice. Diidnt know I could.. Will do so infuture. Thanks Commented Jul 10, 2012 at 21:25

2 Answers 2

1

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"

Sign up to request clarification or add additional context in comments.

Comments

0

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. )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.