Click here to Skip to main content
15,902,635 members

Comments by Member 2716158 (Top 2 by date)

Member 2716158 23-May-11 5:52am View    
Dim objMgmtClass As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMgmtObjCollection As ManagementObjectCollection = objMgmtClass.GetInstances()

For Each MgmtObj As ManagementObject In objMgmtObjCollection
If Not CBool(MgmtObj("IPEnabled")) Then
Continue For
End If

For Each SystemIPAddress As String In MgmtObj("IPAddress")
If SystemIPAddress.Equals(TheCurrentNicIP) Then
Try

'Add the IP alias to the configuration NIC
Dim objNewIP As ManagementBaseObject = Nothing
Dim objSetIP As ManagementBaseObject = Nothing
Dim objNewGate As ManagementBaseObject = Nothing
objNewIP = MgmtObj.GetMethodParameters("EnableStatic")
objNewGate = MgmtObj.GetMethodParameters("SetGateways")

objNewGate("DefaultIPGateway") = New String() {Gateway}
objNewGate("GatewayCostMetric") = New Integer() {1}
objNewIP("IPAddress") = New String() {TheCurrentNicIP, DeviceIPAddress}
objNewIP("SubnetMask") = New String() {TheCurrentNicSubnetMask, DeviceSubnetMask}

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

Console.WriteLine("Updated IPAddress, SubnetMask and Gateway!")

Catch ex As Exception
Throw ex

Finally
If Not IsNothing(objMgmtClass) Then
objMgmtClass.Dispose()
objMgmtClass = Nothing
End If
If Not IsNothing(MgmtObj) Then
MgmtObj.Dispose()
MgmtObj = Nothing
End If
If Not IsNothing(objMgmtObjCollection) Then
objMgmtObjCollection.Dispose()
objMgmtObjCollection = Nothing
End If

End Try
Else
Console.WriteLine("Cannot find the adapter using the following IP Address: " & TheCurrentNicIP

End If
Next
Next
Member 2716158 18-May-11 6:02am View    
I have a windows application through which I would like to add IP Address with Subnet Mask programatically to communicate with external devices such as routers.

How to add IP Address with Subnet Mask programatically using VB.NET in Visual Studio 2010?