Imports System.Management
Imports System.Net
Imports System.Net.NetworkInformation
Public Class Form1
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()
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)
Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!")
Catch ex As Exception
MessageBox.Show("Unable to Set IP : " & ex.Message)
End Try
Next objMO
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
getip()
Catch ex As Exception
ex.ToString()
End Try
Dim PrimaryNic As New Collection
Dim PNic As String = ""
For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties.GatewayAddresses
If gatewayAddr.Address.ToString <> "0.0.0.0" And networkCard.OperationalStatus.ToString() = "Up" Then
Dim IpAddress As UnicastIPAddressInformation
For Each IpAddress In networkCard.GetIPProperties.UnicastAddresses
TextBox2.Text = (IpAddress.IPv4Mask.ToString)
Next
' Get IP gateway information
TextBox3.Text = (gatewayAddr.Address.ToString)
End If
Next
Next
End Sub
Private Sub getip()
Dim hostname As String = Dns.GetHostName
Dim ip As String = System.Net.Dns.GetHostByName(hostname).AddressList(0).ToString
TextBox1.Text = ip
End Sub
End Class