更改 IP - 选择 LAN-Adapter (.net)
Change IP - choosing LAN-Adapter (.net)
我在我的电脑上找到了(Windows 7 和 10)3 个活动的 LAN 适配器。
以前我用的是netsh,因为可以选择适配器
netsh interface ip set address ""LAN-BRIDGED"" static 192.168.255.130 255.255.255.128 192.168.255.129", AppWinStyle.Hide, True)
但有时 netsh 不起作用...所以这就是我不想使用 netsh 的原因。
现在换个IP+子网+网关试试。如果我只激活其中一个 LAN 适配器,我的代码就可以工作。但是,如果它们都处于活动状态,那么它会更改随机 LAN 适配器的 IP。
如何使用我的代码选择一个 LAN 适配器?
Option Strict On
Imports System.Net.NetworkInformation
Imports System.Management
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim LAN_Adapter As NetworkInterface
ComboBoxAdapterSelector.Items.Clear()
For Each LAN_Adapter In NetworkInterface.GetAllNetworkInterfaces()
With LAN_Adapter
ComboBoxAdapterSelector.Items.Add(.Name)
End With
Next
End Sub
Private Sub ChangechoosenIPButton_Click(sender As Object, e As EventArgs) Handles ChangechoosenIPButton.Click
ChangechoosenIP()
End Sub
Sub ChangechoosenIP()
Dim IPAddress As String = TextBoxIPAddress.Text
Dim SubnetMask As String = TextBoxSubnetMask.Text
Dim Gateway As String = TextBoxGateway.Text
If ComboBoxAdapterSelector.SelectedText = "Ethernet 2" Then
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
Try
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewGate As ManagementBaseObject = Nothing
objNewIP = objMO.GetMethodParameters("EnableStatic")
objNewGate = objMO.GetMethodParameters("SetGateways")
objNewGate("DefaultIPGateway") = New String() {Gateway}
objNewGate("GatewayCostMetric") = New Integer() {1}
objNewIP("IPAddress") = New String() {IPAddress}
objNewIP("SubnetMask") = New String() {SubnetMask}
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)
Catch ex As Exception
MessageBox.Show("Error : " & ex.Message)
End Try
Next objMO
ElseIf ComboBoxAdapterSelector.SelectedText = "Ethernet" Then
'.
'.
'.
ElseIf ComboBoxAdapterSelector.SelectedText = "LAN-Connection" Then
'.
'.
'.
End If
End Sub
我在 (LAN and Wireless)
上尝试此代码
For Each objMO As ManagementObject In objMOC
If objMO.SystemProperties("MACAddress").Value IsNot Nothing Then
'***** USE THIS
If objMO.SystemProperties("Description").Value <> "RAS Async Adapter" Then
MessageBox.Show("Caption: " & objMO.SystemProperties("Caption").Value)
'your code
End If
'***** OR THIS
'If objMO.SystemProperties("IPEnabled").Value = True And objMO.SystemProperties("DefaultIPGateway").Value IsNot Nothing Then
' MessageBox.Show("Caption: " & objMO.SystemProperties("Caption").Value)
' 'your code
'End If
End If
Next
A :这是列出所有可用适配器(硬件 w/without 软件)
的方法
Dim HardwareOnly As Boolean = True
For Each LAN_Adapter As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
With LAN_Adapter
If HardwareOnly = True Then 'According to MAC-Address
If LAN_Adapter.GetPhysicalAddress.ToString <> "" Then
If LAN_Adapter.GetPhysicalAddress.ToString.StartsWith("00000000") = False Then
ComboBoxAdapterSelector.Items.Add(LAN_Adapter)
End If
End If
Else
ComboBoxAdapterSelector.Items.Add(LAN_Adapter)
End If
End With
Next
ComboBoxAdapterSelector.DisplayMember = "Name"
B : Now each item in ComboBoxAdapterSelector
is refering to an
adapter (NetworkInterface object)
'so using If ComboBoxAdapterSelector.SelectedText = "Ethernet 2" Then
is not
recommanded
'LAN_Adapter.Name = "Ethernet 2"
is not a static
field
the user can change it from the Control Panel\Network and Internet\Network Connections
If ComboBoxAdapterSelector.SelectedItem IsNot Nothing Then
Dim tmpAdapter As NetworkInterface = DirectCast(ComboBoxAdapterSelector.SelectedItem, NetworkInterface)
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
'choose a static field to compare `objMO` with selected adapter => `tmpAdapter` <br>
'for example `tmpAdapter.Description` `tmpAdapter.GetPhysicalAddress` `tmpAdapter.Id` etc
If objMO.GetPropertyValue("SettingID") = tmpAdapter.Id Then
'NOW you find the object that refers to what you select in ComboBoxAdapterSelector
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
'your code
Exit For
End If
Next objMO
End If
我在我的电脑上找到了(Windows 7 和 10)3 个活动的 LAN 适配器。
以前我用的是netsh,因为可以选择适配器
netsh interface ip set address ""LAN-BRIDGED"" static 192.168.255.130 255.255.255.128 192.168.255.129", AppWinStyle.Hide, True)
但有时 netsh 不起作用...所以这就是我不想使用 netsh 的原因。
现在换个IP+子网+网关试试。如果我只激活其中一个 LAN 适配器,我的代码就可以工作。但是,如果它们都处于活动状态,那么它会更改随机 LAN 适配器的 IP。
如何使用我的代码选择一个 LAN 适配器?
Option Strict On
Imports System.Net.NetworkInformation
Imports System.Management
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim LAN_Adapter As NetworkInterface
ComboBoxAdapterSelector.Items.Clear()
For Each LAN_Adapter In NetworkInterface.GetAllNetworkInterfaces()
With LAN_Adapter
ComboBoxAdapterSelector.Items.Add(.Name)
End With
Next
End Sub
Private Sub ChangechoosenIPButton_Click(sender As Object, e As EventArgs) Handles ChangechoosenIPButton.Click
ChangechoosenIP()
End Sub
Sub ChangechoosenIP()
Dim IPAddress As String = TextBoxIPAddress.Text
Dim SubnetMask As String = TextBoxSubnetMask.Text
Dim Gateway As String = TextBoxGateway.Text
If ComboBoxAdapterSelector.SelectedText = "Ethernet 2" Then
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
Try
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewGate As ManagementBaseObject = Nothing
objNewIP = objMO.GetMethodParameters("EnableStatic")
objNewGate = objMO.GetMethodParameters("SetGateways")
objNewGate("DefaultIPGateway") = New String() {Gateway}
objNewGate("GatewayCostMetric") = New Integer() {1}
objNewIP("IPAddress") = New String() {IPAddress}
objNewIP("SubnetMask") = New String() {SubnetMask}
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)
Catch ex As Exception
MessageBox.Show("Error : " & ex.Message)
End Try
Next objMO
ElseIf ComboBoxAdapterSelector.SelectedText = "Ethernet" Then
'.
'.
'.
ElseIf ComboBoxAdapterSelector.SelectedText = "LAN-Connection" Then
'.
'.
'.
End If
End Sub
我在 (LAN and Wireless)
For Each objMO As ManagementObject In objMOC
If objMO.SystemProperties("MACAddress").Value IsNot Nothing Then
'***** USE THIS
If objMO.SystemProperties("Description").Value <> "RAS Async Adapter" Then
MessageBox.Show("Caption: " & objMO.SystemProperties("Caption").Value)
'your code
End If
'***** OR THIS
'If objMO.SystemProperties("IPEnabled").Value = True And objMO.SystemProperties("DefaultIPGateway").Value IsNot Nothing Then
' MessageBox.Show("Caption: " & objMO.SystemProperties("Caption").Value)
' 'your code
'End If
End If
Next
A :这是列出所有可用适配器(硬件 w/without 软件)
的方法Dim HardwareOnly As Boolean = True
For Each LAN_Adapter As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
With LAN_Adapter
If HardwareOnly = True Then 'According to MAC-Address
If LAN_Adapter.GetPhysicalAddress.ToString <> "" Then
If LAN_Adapter.GetPhysicalAddress.ToString.StartsWith("00000000") = False Then
ComboBoxAdapterSelector.Items.Add(LAN_Adapter)
End If
End If
Else
ComboBoxAdapterSelector.Items.Add(LAN_Adapter)
End If
End With
Next
ComboBoxAdapterSelector.DisplayMember = "Name"
B : Now each item in
ComboBoxAdapterSelector
is refering to an adapter (NetworkInterface object)
'so usingIf ComboBoxAdapterSelector.SelectedText = "Ethernet 2" Then
is not recommanded
'LAN_Adapter.Name = "Ethernet 2"
is not a static field
the user can change it from theControl Panel\Network and Internet\Network Connections
If ComboBoxAdapterSelector.SelectedItem IsNot Nothing Then
Dim tmpAdapter As NetworkInterface = DirectCast(ComboBoxAdapterSelector.SelectedItem, NetworkInterface)
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
'choose a static field to compare `objMO` with selected adapter => `tmpAdapter` <br>
'for example `tmpAdapter.Description` `tmpAdapter.GetPhysicalAddress` `tmpAdapter.Id` etc
If objMO.GetPropertyValue("SettingID") = tmpAdapter.Id Then
'NOW you find the object that refers to what you select in ComboBoxAdapterSelector
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
'your code
Exit For
End If
Next objMO
End If