使用vb.net更改dns地址

问题描述:

如何在visual basi.net中改变这两个值。我不想使用vbscript来执行此操作。

how to change this two values in visual basi.net. I do not want to use vbscript to do this.

我搜索并获取 - 如何使用C#中的代码更改网络设置(IP地址,DNS,WINS,主机名) / a>

i searched and get this - How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#

^转换代码:

''' <summary>
''' Set's a new IP Address and it's Submask of the local machine
''' </summary>
''' <param name="ip_address">The IP Address</param>
''' <param name="subnet_mask">The Submask IP Address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setIP(ByVal ip_address As String, ByVal subnet_mask As String)

    Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If CBool(objMO("IPEnabled")) Then
            Try
                Dim setIP As ManagementBaseObject
                Dim newIP As ManagementBaseObject = objMO.GetMethodParameters("EnableStatic")

                newIP("IPAddress") = New String() {ip_address}
                newIP("SubnetMask") = New String() {subnet_mask}

                setIP = objMO.InvokeMethod("EnableStatic", newIP, Nothing)
            Catch generatedExceptionName As Exception
                Throw


            End Try
        End If
    Next
End Sub
''' <summary>
''' Set's a new Gateway address of the local machine
''' </summary>
''' <param name="gateway">The Gateway IP Address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setGateway(ByVal gateway As String)
    Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If CBool(objMO("IPEnabled")) Then
            Try
                Dim setGateway As ManagementBaseObject
                Dim newGateway As ManagementBaseObject = objMO.GetMethodParameters("SetGateways")

                newGateway("DefaultIPGateway") = New String() {gateway}
                newGateway("GatewayCostMetric") = New Integer() {1}

                setGateway = objMO.InvokeMethod("SetGateways", newGateway, Nothing)
            Catch generatedExceptionName As Exception
                Throw
            End Try
        End If
    Next
End Sub
''' <summary>
''' Set's the DNS Server of the local machine
''' </summary>
''' <param name="NIC">NIC address</param>
''' <param name="DNS">DNS server address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
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 CBool(objMO("IPEnabled")) Then
            ' if you are using the System.Net.NetworkInformation.NetworkInterface you'll need to change this line to if (objMO["Caption"].ToString().Contains(NIC)) and pass in the Description property instead of the name 
            If objMO("Caption").Equals(NIC) Then
                Try
                    Dim newDNS As ManagementBaseObject = objMO.GetMethodParameters("SetDNSServerSearchOrder")
                    newDNS("DNSServerSearchOrder") = DNS.Split(","c)
                    Dim setDNS As ManagementBaseObject = objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, Nothing)
                Catch generatedExceptionName As Exception
                    Throw
                End Try
            End If
        End If
    Next
End Sub
''' <summary>
''' Set's WINS of the local machine
''' </summary>
''' <param name="NIC">NIC Address</param>
''' <param name="priWINS">Primary WINS server address</param>
''' <param name="secWINS">Secondary WINS server address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setWINS(ByVal NIC As String, ByVal priWINS As String, ByVal secWINS As String)
    Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

    For Each objMO As ManagementObject In objMOC
        If CBool(objMO("IPEnabled")) Then
            If objMO("Caption").Equals(NIC) Then
                Try
                    Dim setWINS As ManagementBaseObject
                    Dim wins As ManagementBaseObject = objMO.GetMethodParameters("SetWINSServer")
                    wins.SetPropertyValue("WINSPrimaryServer", priWINS)
                    wins.SetPropertyValue("WINSSecondaryServer", secWINS)

                    setWINS = objMO.InvokeMethod("SetWINSServer", wins, Nothing)
                Catch generatedExceptionName As Exception
                    Throw
                End Try
            End If
        End If
    Next
End Sub

但在ManagementClass()和其他项目。我导入了System.Management。但是vb显示没有找到错误。

but gives error in ManagementClass() and other items. I imported System.Management. But vb shows error that it is not found.

这是我转换为在pc中打印nic的代码。这是对的吗? :

This is code i converted to print nic available in a pc. Is it correct? :

    For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
        If nic.OperationalStatus = OperationalStatus.Up Then
            Debug.Print(nic.GetPhysicalAddress().ToString())
            Exit For
        End If
    Next

但是是供应的nic名称?或者使用示例演示?

but was is nic name to supply? or a example demo to use it?

您已经找到有些人很擅长为你写大部分代码a>,现在你要做的就是把它翻译成VB.NET。鉴于两种语言的广泛相似之处,对于任何真实的.NET程序员来说,这并不难。但是,如果您需要一些额外的帮助,请尝试使用免费的在线翻译器,如开发人员融合中的一个。

然后唯一剩下的问题是如何知道要提供的NIC名称,因为链接代码接受 string 参数指定NIC。

The only remaining issue, then, is how you know which NIC name to supply, since the linked code accepts a string parameter specifying the NIC.

不幸的是,这不是一个问题,我们(或任何人)可以给你一个确切的答案。问题是单个计算机可以安装多个NIC,并且每个计算机可以具有不同的DNS设置。这就是为什么该函数需要参数,以便您可以选择要配置的参数。

Unfortunately, that's not a question to which we (or anyone) can give you an exact answer. The issue is that a single computer can have multiple NICs installed, and each of them can have different DNS settings. That's why the function requires the parameter so that you can choose which one to configure.

简单直接的方法是枚举所有安装的NIC,然后

The simple, straightforward approach is to enumerate all of the installed NICs, then


  • 如果只有一个,使用那个。

  • 如果有多个,显示一个对话框列出每个的名称并要求用户选择。

观察链接的代码使用 Caption 属性的$ code> Win32_NetworkAdapterConfiguration 类作为NIC的名称。这可能与您应该向用户显示的名称一样,以及您认为可能包含有用信息的任何其他属性值,以帮助他们作出决定。

Observe that the linked code uses the Caption property of the Win32_NetworkAdapterConfiguration class as the "name" of the NIC. This is probably the same name that you should display to the user, along with any of the other property values that you think might contain useful information to help them in making a decision.

要求用户几乎总是一个cop-out方法,考虑到用户不在乎,很可能不知道答案。良好的UI设计将这样的东西保持在最低限度。但在这里,你真的没有太多的选择。使用逻辑来合理化选择,如果用户在计算机上安装了多个NIC,那么她可能是一个高级用户,他将会知道如何回答您的问题。

Asking the user is almost always a cop-out approach, considering that the user does not care and most likely does not know the answer. Good UI design keeps stuff like this to a bare minimum. But here, you really do not have much choice. Rationalize the choice with the logic that if a user has more than one NIC installed on her computer, she is probably an advanced user who will know how to answer your question.

修改代码以枚举所有安装的NIC是相对简单的。从 Marc的重构代码开始:

Modifying the code to enumerate all of the installed NICs is relatively straightforward. Starting from Marc's refactored code:

foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"] && objMO["Caption"].Equals(nic)))

我们只需要删除应用于 Caption property:

we just need to remove the equality test applied to the Caption property:

foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"]))