【问题标题】:change dns address using vb.net使用 vb.net 更改 dns 地址
【发布时间】:2013-03-15 07:54:13
【问题描述】:

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

我搜索并得到了这个 - 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显示没有找到的错误。

这是我转换为可在 pc 中使用的打印 nic 的代码。这是正确的吗? :

    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 名称吗?或使用它的示例演示?

【问题讨论】:

    标签: vb.net dns


    【解决方案1】:

    您已经找到some people who were nice enough to write most of the code for you,现在您只需将其翻译成VB.NET。考虑到这两种语言之间的广泛相似性,这对任何真正的 .NET 程序员来说都不难。但是,如果您需要一点额外帮助,请尝试使用免费的在线翻译器之一,例如 this one from Developer Fusion

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

    很遗憾,我们(或任何人)无法为您提供准确的答案。问题是一台计算机可以安装多个 NIC,每个 NIC 都可以有不同的 DNS 设置。这就是为什么该函数需要参数,以便您可以选择配置哪一个。

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

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

    观察到链接代码使用Win32_NetworkAdapterConfiguration 类的Caption 属性作为NIC 的“名称”。这可能是您应该向用户显示的相同名称,以及您认为可能包含有助于他们做出决定的有用信息的任何其他属性值。

    Asking the user is almost always a cop-out approach,考虑到用户不关心并且很可能不知道答案。良好的 UI 设计将这样的东西保持在最低限度。但在这里,你真的没有太多选择。合理化选择的逻辑是,如果用户在她的计算机上安装了多个 NIC,那么她可能是一位知道如何回答您的问题的高级用户。

    修改代码以枚举所有已安装的 NIC 相对简单。从Marc's refactored code开始:

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

    我们只需要删除应用于Caption 属性的相等性测试:

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

    【讨论】:

    • thnq。我添加了转换后的代码。但是 vb 显示 Management*() 的类型未定义错误
    • @VppMan 是的,正如 XML 文档的 remarks 部分所述,您需要添加对 System.Management namespace 的项目引用。代码使用了在这个命名空间中定义的类,这些类封装了 WMI 接口。
    • tnqu 科迪。我要测试它。如果它有效,那么我可以在 dns 地址中看到新值(在我的问题中上图中标记的部分)。对吗?
    【解决方案2】:

    您需要引用 System.management 以及导入它。

    【讨论】:

    • 请附上您的参考资料和操作步骤。
    猜你喜欢
    • 2014-08-11
    • 2013-01-24
    • 2021-11-02
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    相关资源
    最近更新 更多