0

I am using this code to change Ip Address, Subnet Mask and Default Gateway but it seems only the default gateway changes and the IP address and subnet mask doesnt. Can you please help me? I am using windows 7

Dim IPAddress As String = "192.168.2.130"
Dim SubnetMask As String = "255.0.0.0"
Dim Gateway As String = "192.168.2.1"
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)

        MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway!")
    Catch ex As Exception
        MessageBox.Show("Unable to Set IP : " & ex.Message)
    End Try
Next objMO
Tim
  • 28,212
  • 8
  • 63
  • 76

1 Answers1

1

Right click your exe in bin and run as an administrator because changing an IP address needs an administrator rights and in Windows 7 to have that kind of rights you have to run it as an admin.

Edper
  • 9,144
  • 1
  • 27
  • 46
  • This would be better as a comment. If you want to make it an answer, it would be a **better** answer if you explained why running the exe as an administrator would resolve the issue. – Tim Apr 21 '13 at 02:45
  • Thank you @Tim. I am answering using my ipad so which means the less number of characters i could write the better. And i am editing my answer according to your request. Thanks once again. – Edper Apr 21 '13 at 03:02
  • No problem. Thanks for editing and adding more to the answer. I'm just one of those guys who always wants to know the why of things...I'm pretty sure I drove my parents nuts when I was a kid :) Plus someone other than OP may need this answer in the future and having more details is almost always helpful. – Tim Apr 21 '13 at 03:06