2

I'm aware of how to do this via cmd/executing cmd from vb.net, but I'd prefer to just do it through vb.net.

The following code works to manually set the primary and secondary dns addresses for IPv4, what would I need to modify to also programmatically set an IPv6 primary and secondary DNS address?

Any Help would be greatly appreciated!

'Requires admin privileges and a reference to System.Management
Imports System.Management
Imports System.Net.NetworkInformation

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SetDNSOnAllActiveNetworkCards()
End Sub

Public Sub SetDNSOnAllActiveNetworkCards()

    For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
        For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties.GatewayAddresses
            Try
                If gatewayAddr.Address.ToString <> "0.0.0.0" And networkCard.OperationalStatus.ToString() = "Up" Then
                    setDNS(networkCard.Description, "8.8.8.8,8.8.4.4")
                End If
            Catch ex As Exception
                MessageBox.Show("An error occured while setting your IPv4 dns address")
            End Try
        Next
    Next

End Sub

Public Sub setDNS(ByVal NIC As String, ByVal DNS As String)
    Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If objMO("Caption").ToString.Contains(NIC) Then
            Try
                Dim newDNS As ManagementBaseObject = objMO.GetMethodParameters("SetDNSServerSearchOrder")
                newDNS("DNSServerSearchOrder") = DNS.Split(","c)
                Dim setDNS As ManagementBaseObject = objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, Nothing)
                MessageBox.Show("Your IPv4 dns address has been successfully set!")
            Catch ex As Exception
                MessageBox.Show("An error occured while setting your IPv4 dns address")
            End Try
        End If
    Next

End Sub

End Class

Edit

This is how I would set the IPv6 address using CMD, however I do not want to use other programs in order to set the addresses, I would rather have it completely set from VB.net just like the IPv4 address.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim CmdCommands As String = "@Echo Off & "
    CmdCommands += "color 0b & "

    CmdCommands += "Echo Setting IPv6 DNS Address & "

    For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
        For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties.GatewayAddresses

            If gatewayAddr.Address.ToString <> "0.0.0.0" And networkCard.OperationalStatus.ToString() = "Up" Then
                CmdCommands += "netsh interface ipv6 add dnsserver """ & networkCard.Name & """ 2001:4860:4860::8888 index=1 validate=yes >nul 2>nul & "
                CmdCommands += "netsh interface ipv6 add dnsserver """ & networkCard.Name & """ 2001:4860:4860::8844 index=2 validate=yes >nul 2>nul & "
            End If

        Next
    Next
    CmdCommands += "@Echo. & "
    CmdCommands += "Echo Finished! Press any key to exit. & "
    CmdCommands += "pause >nul 2>nul & "
    CmdCommands += "exit"

    Dim PSI As New ProcessStartInfo("cmd", "/C " & CmdCommands)
    PSI.UseShellExecute = True
    PSI.WindowStyle = ProcessWindowStyle.Normal
    PSI.Verb = "runas"
    Process.Start(PSI)

End Sub
User7071
  • 77
  • 5
  • 1
    What have you tried to set the IPv6 DNS address? – Blackwood Nov 06 '17 at 23:26
  • 1
    @Blackwood From purely VB.net? Nothing as i'm not sure how to, hence this post. However I have updated the OP to show how I updated the IPv6 address using CMD (which I do not want to use, I would like the method to be completely from vb.net instead of executing other programs) – User7071 Nov 06 '17 at 23:55
  • 1
    While I do not have any actual sources to share other than [this blog](https://www.saotn.org/wminetsh-add-dns-servers-network-adapters/#WMI_and_netsh_to_add_DNS_servers_on_network_adapters_for_IPv4_and_IPv6_addresses), from the research I've made it does not seem to be possible to set an IPv6 DNS via WMI. I can definitely be wrong about this as I haven't made a complete and extensive search, but I just thought I'd let you know that in my opinion I wouldn't get my hopes up. :/ – Visual Vincent Nov 07 '17 at 00:09

0 Answers0