0

在此处输入图像描述

如何在 visual basi.net 中更改这两个值。我不想使用 vbscript 来做到这一点。

我搜索并得到了这个 -如何使用 C# 中的代码更改网络设置(IP 地址、DNS、WINS、主机名)

^ 转换后的代码:

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

4

2 回答 2

1

您已经找到了一些人很好,他们可以为您编写大部分代码,现在您所要做的就是将其转换为 VB.NET。考虑到这两种语言之间的广泛相似性,这对任何真正的 .NET 程序员来说都不难。但是,如果您需要一些额外的帮助,请尝试使用免费的在线翻译器之一,例如 Developer Fusion 中的这个

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

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

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

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

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

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

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

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"]))
于 2013-03-15T08:29:46.513 回答
0

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

于 2013-07-02T01:34:56.677 回答