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