Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

Any inbuilt methods provided in VS2010? Please suggest.

Please help me with sample code.
Posted
Updated 18-May-11 0:01am
v2

1 solution

IP Alias? Describe what it is you're talking about.

There is nothing in Visual Studio, no, because Visual Studio is used to write an debug code. It has nothing to do with networking at all.

The .NET Framework doesn't supply any native classes or methods to enable an "IP Alias".

Perhaps a better explanation of what this is and the steps you take to do this manually would help.
 
Share this answer
 
Comments
Member 2716158 18-May-11 6:02am    
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?
Dave Kreskowiak 18-May-11 10:08am    
You would have to do this with the System.Management classes and WMI. Read up on the Win32_NetworkAdapterConfiguration class. You'll have to get the addresses, subnets and gateways that already exist, add your new address information to those lists, then feed those arrays back to the Win32_NetworkAdapterConfiguration class' EnableStatic and SetGateways methods.

No, I don't have an example. I haven't had to do this in about 10 years, but that's how it's done.
Member 2716158 23-May-11 5:52am    
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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900