在 VB.net 中设置 IP 和 DNS 以使用来自 WMI 的 DHCP
Set IP and DNS to use DHCP from WMI in VB.net
我正在尝试通过 vb.net 程序与 WMI 进行交互,以便让运行该程序的任何计算机从 DHCP 为所有具有 IP 的网络适配器提取 IP 设置和 DNS 服务器设置。
我目前拥有的代码可毫无问题地用于 DHCP,但不会更改 DNS 设置。该程序编译和执行没有问题,但 DNS 设置没有更改为自动从 DHCP 获取。
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If (Not CBool(objMO("IPEnabled"))) Then
Continue For
End If
Try
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewDNS As ManagementBaseObject = Nothing
Dim objSetDNS As ManagementBaseObject = Nothing
objNewIP = objMO.GetMethodParameters("EnableDHCP")
objSetIP = objMO.InvokeMethod("EnableDHCP", Nothing, Nothing)
objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")
objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", Nothing, Nothing)
Catch ex As Exception
MessageBox.Show("Settings unchanged : " & ex.Message)
End Try
Next objMO
我已经快要解决这个问题了,我只需要帮助来解决最后一步。
你必须通过注册表来做这很烦人,他们没有为它添加 WMI 方法。具体来说(取自此处:https://gallery.technet.microsoft.com/7b1cec46-bdb8-4afc-b240-9789eefce6de)您需要将此键设置为空。
"HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
下面是您的代码,其中插入了必要的新子
Const conKeyPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
Public Sub Test()
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If (Not CBool(objMO("IPEnabled"))) Then
Continue For
End If
Try
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewDNS As ManagementBaseObject = Nothing
Dim objSetDNS As ManagementBaseObject = Nothing
objNewIP = objMO.GetMethodParameters("EnableDHCP")
objSetIP = objMO.InvokeMethod("EnableDHCP", Nothing, Nothing)
objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")
objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", Nothing, Nothing)
SetDNSAutomatically(objMO.GetPropertyValue("settingID"))
Catch ex As Exception
MessageBox.Show("Settings unchanged : " & ex.Message)
End Try
Next objMO
End Sub
Private Sub SetDNSAutomatically(ByVal settingID As String)
If settingID = String.Empty Then
Throw New ArgumentNullException("settingID")
End If
Dim _adapterKeyPath = String.Format("{0}\{1}", conKeyPath, settingID)
My.Computer.Registry.SetValue(_adapterKeyPath, "NameServer", String.Empty)
End Sub
我正在尝试通过 vb.net 程序与 WMI 进行交互,以便让运行该程序的任何计算机从 DHCP 为所有具有 IP 的网络适配器提取 IP 设置和 DNS 服务器设置。
我目前拥有的代码可毫无问题地用于 DHCP,但不会更改 DNS 设置。该程序编译和执行没有问题,但 DNS 设置没有更改为自动从 DHCP 获取。
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If (Not CBool(objMO("IPEnabled"))) Then
Continue For
End If
Try
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewDNS As ManagementBaseObject = Nothing
Dim objSetDNS As ManagementBaseObject = Nothing
objNewIP = objMO.GetMethodParameters("EnableDHCP")
objSetIP = objMO.InvokeMethod("EnableDHCP", Nothing, Nothing)
objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")
objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", Nothing, Nothing)
Catch ex As Exception
MessageBox.Show("Settings unchanged : " & ex.Message)
End Try
Next objMO
我已经快要解决这个问题了,我只需要帮助来解决最后一步。
你必须通过注册表来做这很烦人,他们没有为它添加 WMI 方法。具体来说(取自此处:https://gallery.technet.microsoft.com/7b1cec46-bdb8-4afc-b240-9789eefce6de)您需要将此键设置为空。
"HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
下面是您的代码,其中插入了必要的新子
Const conKeyPath = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
Public Sub Test()
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If (Not CBool(objMO("IPEnabled"))) Then
Continue For
End If
Try
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewDNS As ManagementBaseObject = Nothing
Dim objSetDNS As ManagementBaseObject = Nothing
objNewIP = objMO.GetMethodParameters("EnableDHCP")
objSetIP = objMO.InvokeMethod("EnableDHCP", Nothing, Nothing)
objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder")
objSetDNS = objMO.InvokeMethod("SetDNSServerSearchOrder", Nothing, Nothing)
SetDNSAutomatically(objMO.GetPropertyValue("settingID"))
Catch ex As Exception
MessageBox.Show("Settings unchanged : " & ex.Message)
End Try
Next objMO
End Sub
Private Sub SetDNSAutomatically(ByVal settingID As String)
If settingID = String.Empty Then
Throw New ArgumentNullException("settingID")
End If
Dim _adapterKeyPath = String.Format("{0}\{1}", conKeyPath, settingID)
My.Computer.Registry.SetValue(_adapterKeyPath, "NameServer", String.Empty)
End Sub