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

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

Dani AI

Generated

’s snippet already covers EnableStatic and SetGateways, which is why DNS entries never get applied: the WMI method for DNS must be called explicitly. For Win32_NetworkAdapterConfiguration you need to invoke SetDNSServerSearchOrder and pass a string array of DNS servers. Run the process elevated (Administrator) and only touch adapters with IPEnabled = True. ’s suggestion to verify results with ipconfig /all is a good follow-up after applying changes.

Example (VB.NET — only the DNS part):

' requires: Imports System.Management
Dim servers As String() = New String() {txtdns1.Text.Trim(), txtdns2.Text.Trim()}

Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
For Each mo As ManagementObject In mc.GetInstances()
    If Not CBool(mo("IPEnabled")) Then Continue For

    Dim dnsParams As ManagementBaseObject = mo.GetMethodParameters("SetDNSServerSearchOrder")
    dnsParams("DNSServerSearchOrder") = servers
    Dim outParams As ManagementBaseObject = mo.InvokeMethod("SetDNSServerSearchOrder", dnsParams, Nothing)

    Dim ret As Integer = Convert.ToInt32(outParams("ReturnValue"))
    If ret <> 0 Then
        ' log ret for troubleshooting
    End If
Next

Alternatives: use PowerShell on modern Windows (example: Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses ("8.8.8.8","8.8.4.4")) or netsh on older systems if WMI/permissions are problematic. When automating, identify the exact adapter by Description or Index (from the ManagementObject) to avoid changing VPN/virtual adapters.

Troubleshooting checklist: run the app elevated (UAC), confirm IPEnabled = True, check the method return code and Windows Event Log, verify with ipconfig /all, and flush DNS cache (ipconfig /flushdns) if needed.

If you have a Windows PC, You can get this information by running “ipconfig /all” from the cmd window. Click on Start from the taskbar and select “Run”. Then enter CMD and click “OK”.

(i). Type “ipconfig /all” and hit “Enter”.
(ii). You should see a screen that looks similar to the one below this section.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.