如何以编程方式设置IPv6 DNS地址?

时间:2017-11-06 22:36:52

标签: vb.net

我知道如何通过cmd /从vb.net执行cmd来做到这一点,但我更喜欢通过vb.net来做。

以下代码适用于手动设置IPv4的主要和辅助DNS地址,我还需要修改哪些以编程方式设置IPv6主DNS地址和辅助DNS地址?

非常感谢任何帮助!

'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

修改

这就是我使用CMD设置IPv6地址的方法,但是我不想使用其他程序来设置地址,我宁愿从VB.net完全设置它就像IPv4地址一样。

 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

0 个答案:

没有答案