Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i configure ip address, submask, getway, DNS1 and also DNS2 please help me
i use this code but DNS not added what is the actual code for add all with DNS1 &2
Please help me

What I have tried:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
     Button2_Click(sender, e)
     ' TxtNumIP.Text = Label1.Text
     Dim IPAddress As String = TxtNumIP.Text.Trim '192.168.1.6
     Dim SubnetMask As String = TxtSubnet.Text.Trim '"255.255.255.0" ' TxtSubnet.Text.Trim '255.255.255.0
     Dim Gateway As String = TxtGateway.Text.Trim  '192.168.1.1
     Dim DNS1 As String = txtdns1.Text.Trim
     Dim DNS2 As String = txtdns2.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

         Try
             Dim objNewIP As ManagementBaseObject = Nothing
             Dim objSetIP As ManagementBaseObject = Nothing
             Dim objNewGate As ManagementBaseObject = Nothing
             Dim objdhcp 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)

             ' Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!")

         Catch ex As Exception
             ' MessageBox.Show("Unable to Set IP : " & ex.Message)
         End Try
     Next objMO
 End Sub
Posted
Updated 23-Aug-22 4:39am

1 solution

to get your current setup. go to a command line on a working network computer.

Type

ipconfig

or

ipconfig /?

The second will give you options to use and look up. I believe the option /all will give you the dns server entries.
 
Share this answer
 
Comments
Jayanta Modak 27-Aug-22 11:27am    
Thanks sir for your reply. But any vb code for this I add ip, submask, gateway but can't add DNS 1 and DNS 2 please help me

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