Click here to Skip to main content
15,391,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Hoe to chnage ip address,subnetmask,gateway,both dns for server machine form server machin or client machine using in vb.

I will try upto ip,subnet and gateway and it change ,below is my code,i need dns also need to change by user input.

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       'Dim IPAddress As String = ""
       'Dim SubnetMask As String = ""
       'Dim Gateway As String = ""

       Dim IPAddress As String = TxtNumIP.Text.Trim
       Dim SubnetMask As String = TxtSubnet.Text.Trim
       Dim Gateway As String = TxtGateway.Text.Trim

       Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
       Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

       For Each objMO As ManagementObject In objMOC
           If (Not CBool(objMO("IPEnabled"))) Then
               Continue For
           End If

               Dim objNewIP As ManagementBaseObject = Nothing
               Dim objSetIP As ManagementBaseObject = Nothing
               Dim objNewGate As ManagementBaseObject = Nothing

               objNewIP = objMO.GetMethodParameters("EnableStatic")
               objNewGate = objMO.GetMethodParameters("SetGateways")

               'Set DefaultGateway
               objNewGate("DefaultIPGateway") = New String() {Gateway}
               objNewGate("GatewayCostMetric") = New Integer() {1}

               'Set IPAddress and Subnet Mask
               objNewIP("IPAddress") = New String() {IPAddress}
               objNewIP("SubnetMask") = New String() {SubnetMask}

               objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
               objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)

           Catch ex As Exception

           End Try
       Next objMO
   End Sub

After change ip,subnet and getway,DHCP Enabled value automatically changed to "No",so after that i internet not work,if i change DHCp Enabled valu "Yes",then only internet work.

So my need is how change Ip,subnet,gateway and both dns without affect DHCP changed to "No"

Pls reply asap


1 solution

Hi Just i add 2 cmd lines for set preferred and alternate DNS

Dim p As New Process()
Dim psi As New ProcessStartInfo("netsh", "interface ip add dns name=\""Local Area Connection\"" addr='" + DNSP + "' primary")
p.StartInfo = psi
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True

Dim p1 As New Process()
Dim psi1 As New ProcessStartInfo("netsh", "interface ip set dns name=\""Local Area Connection\"" addr='" + DNSS + "' secondary")
p1.StartInfo = psi1
p1.StartInfo.UseShellExecute = False
p1.StartInfo.RedirectStandardOutput = True

DNSP and DNSS i will get form textbox and pass here,if we mention primary and secondary,it will be preferred and alternate dns.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900