Reputation: 27
i got at my pc (Windows 7 and 10) 3 active LAN-Adapters.
In history I used netsh, because you can choose an adapter
netsh interface ip set address ""LAN-BRIDGED"" static 192.168.255.130 255.255.255.128 192.168.255.129", AppWinStyle.Hide, True)
But sometimes netsh doesn´t work... So thats why I don´t want to use netsh.
Now I try it by another way to change IP + Subnet + Gateway. If I only activate one of these LAN-Adapters, my code works. But if all of them active then it changes the IP at a random LAN-Adapter.
How can i choose one exactly LAN-Adapter with my code?
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
Upvotes: 0
Views: 292
Reputation: 576
A : Here is how to list ALL available adapters (Hardware w/without software)
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
Upvotes: 1
Reputation: 576
I try this code on (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
Upvotes: 1