Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 137,226 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,031 people online right now. Registration is fast and FREE... Join Now!





Set the IP address of the local machine

Sets the IP address of the local machine through WMI. That's right! NO UBER API USAGE. Based on code by Logu Krishnan at http://www.codeproject.com/KB/system/cstcpipwmi.aspx

Submitted By: RodgerB
Actions:
Rating:
Views: 1,962

Language: VB.NET

Last Modified: June 6, 2008
Instructions: 1) Add a reference to the System.Management namespace by going to Project > Add Reference.. and selecting the System.Management option in the list.

2) Add 'Imports System.Management' (without the quotation) to the top of your code.

3) Copy and paste the subroutine into a class.
4) Read the example on how to call the function.

Snippet


  1. ''' <summary>
  2. ''' Sets the IP address of the local machine.
  3. ''' </summary>
  4. ''' <param name="IPAddress">The IP address to set the computer to</param>
  5. ''' <param name="SubnetMask">The subnet mask to set the computer to</param>
  6. ''' <param name="Gateway">The gateway to set the computer to</param>
  7. ''' <remarks>Adapted from code by Logu Krishnan at
  8. ''' http://www.codeproject.com/KB/system/cstcpipwmi.aspx
  9. ''' Requires a reference to the System.Management namespace</remarks>
  10. Private Sub SetIP(ByVal IPAddress As String, ByVal SubnetMask As String, _
  11.                   ByVal Gateway As String)
  12.  
  13.     Dim managementClass As New ManagementClass("Win32_NetworkAdapterConfiguration")
  14.     Dim mgObjCollection As ManagementObjectCollection = managementClass.GetInstances()
  15.  
  16.     For Each mgObject As ManagementObject In mgObjCollection
  17.         If Not CType(mgObject("IPEnabled"), Boolean) Then Continue For
  18.  
  19.         Try
  20.             Dim objNewIP As ManagementBaseObject = Nothing
  21.             Dim objSetIP As ManagementBaseObject = Nothing
  22.             Dim objNewGate As ManagementBaseObject = Nothing
  23.  
  24.             objNewIP = mgObject.GetMethodParameters("EnableStatic")
  25.             objNewGate = mgObject.GetMethodParameters("SetGateways")
  26.  
  27.             ' Set the default gateway (decided to declare and initialise
  28.             ' variables rather than attempting to initialize the array
  29.             ' while communicating with the WMI.
  30.             Dim tmpStrArray() As String = {Gateway}
  31.  
  32.             objNewGate("DefaultIPGateway") = tmpStrArray
  33.             Dim tmpIntArray() As Integer = {1}
  34.             objNewGate("GatewayCostMetric") = tmpIntArray
  35.  
  36.             ' Set the IP address and subnet.
  37.             tmpStrArray(0) = IPAddress
  38.             objNewIP("IPAddress") = tmpStrArray
  39.             tmpStrArray(0) = SubnetMask
  40.             objNewIP("SubnetMask") = tmpStrArray
  41.  
  42.             objSetIP = mgObject.InvokeMethod("EnableStatic", objNewIP, Nothing)
  43.             objSetIP = mgObject.InvokeMethod("SetGateways", objNewGate, Nothing)
  44.         Catch ex As Exception
  45.             MessageBox.Show("An error occured: " + ex.Message)
  46.         End Try
  47.     Next
  48. End Sub
  49.  
  50. ' Example Usage
  51. SetIP("192.168.1.230", "255.255.255.0", "192.168.1.51")

Copy & Paste


Comments


glum 2008-10-08 14:12:34

RodgerB, Thanks so much for this code. I was having trouble finding a solution that worked and was using a hackjob with netsh.exe to accomplish the same thing. I've incorporated this code into my Internal "Support Tool" for our company which allows our laptop users to store different network profiles. I have a need to set the DNS Entries also.. I think going down the route of using the method SetDNSServerSearchOrder will get me in the right direction. Once again thanks for your great code.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month