Visual Basic#2010 IP Changer with option to select Network Adapter

  • Thread starter Thread starter turbozob
  • Start date Start date
T

turbozob

Guest
I would like to program a application that changes IP address of network card which can be chosen by dropdown menu. I now have managed to get basic IP change to work, but problem is I do not want to change my Wireless network interface IP, so I would like to choose which network interface to change. This is my code so far. I am not a professional in programming but I have done some applications with help on the internet.

Thanks


Imports System.Management
Imports System.Net

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "192.168.1.123"
TextBox2.Text = "255.255.255.0"
TextBox3.Text = "192.168.1.1"
End Sub

'SET STATIC IP BUTTON
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim IPAddress As String = TextBox1.Text
Dim SubnetMask As String = TextBox2.Text
Dim Gateway As String = TextBox3.Text

Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
'Dim wiredadaptor As ManagementClass = New ManagementClass("Win32_NetworkAdapter instance")

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 objNewGate As ManagementBaseObject = Nothing

objNewIP = objMO.GetMethodParameters("EnableStatic")
objNewGate = objMO.GetMethodParameters("SetGateways")

'Set DefaultGateway
objNewGate("DefaultIPGateway") = New String() {Gateway}
objNewGate("GatewayCostMetric") = New Integer() {1}

'Set IPAddress and Subnet Mask
objNewIP("IPAddress") = New String() {IPAddress}
objNewIP("SubnetMask") = New String() {SubnetMask}

objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)

MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway!")

Catch ex As Exception
MessageBox.Show("Unable to Set IP : " & ex.Message)
End Try
Next objMO
End Sub

'SET DHCP BUTTON
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
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 objNewGate As ManagementBaseObject = Nothing

'objNewIP = objMO.GetMethodParameters("EnableStatic")
'objNewGate = objMO.GetMethodParameters("SetGateways")


objSetIP = objMO.InvokeMethod("EnableDHCP", objNewIP, Nothing)
'objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)

MessageBox.Show("DHCP IS SET!")

Catch ex As Exception
MessageBox.Show("Unable to Set IP : " & ex.Message)
End Try
Next objMO
End Sub

'RETRIEVE IP ADDRESS BUTTON
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim strHostName As String

Dim strIPAddress As String



strHostName = System.Net.Dns.GetHostName()

strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()


MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)
End Sub


End Class

Continue reading...
 
Back
Top