yyhananyy Posted December 1, 2014 Share Posted December 1, 2014 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 ClassAs 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! Link to comment Share on other sites More sharing options...
Kelsenellenelvian Posted December 1, 2014 Share Posted December 1, 2014 Forum rules: 2.c Requesting help with academic material, such as assignments, is not permitted on MSFN. Receiving such help is often viewed as plagiarism in many schools, and therefore cannot be allowed. This is for your own protection in your academic career. Link to comment Share on other sites More sharing options...
Tripredacus Posted December 1, 2014 Share Posted December 1, 2014 Is this one of those "you must use this language and this method only!" type of assignments? I always hated those. Maybe the teachers hate those too when I would solve the problem using something else. Even today, if I were tasked with changing an IP address with VB, I would have no idea where to start. Well no that isn't true. The first step is always to determine what is permitted within the network. If I do manage to change the computer's IP, what am I allowed to change it to? Do I really need to use something more than netsh? What it the address range for accessing this or that network? If I change the IP to something in that range, will it cause a duplicate IP and make some other device loose their network access? We can't provide a solution to your programming assignment or fix your code example. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now