im trying to change my ip using visual basic i'm pretty new to Visual Basic, and i have a small project for school but i can't get it done. hope you guys can show some light in the dark. this is my code:
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 As you can see i made 3 textboxs that retrieve the Static IP, Subnet Mask and Gateway from the components. That way if computer has other data than 192.168 it'll work on it too. But it doesn't work, when i press the button it's not even thinking. just nothing happen. i have also tried to run it from a batch file, it didn't work as well. It changed that ip (to the specific one in the batch file, but the internet connection stopped working) i'm testing it on a Virtual Machine (windows 7) that i have, i didn't want to screw my connection if something will go wrong. (i don't know if it matter that im testing it on VM or not) Hope you can help me Many Thanks!