Get and set IP address of local computer in VB.NET

This VB.NET tip shows you how to get the IP address of your local computer and set an IP address to it. Perhaps there are many ways to accomplish these tasks. In this tip, I use the GetHostEntry method of the Dns (in System.Net assembly) class to get the IP address of the machine. The GetHostEntry method returns a list of IP addresses that matches the given host name. You can get the host name of your machine by using the GetHostName method of the Dns class.
To set an IP address to the local machine, the VB.NET code is written to change the ip value in the registry editor. In my computer that installed Windows 7, the path that stores the ip address is HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ services\Tcpip\Parameters\ Interfaces\  {D21C6DAA-DAAC-4F79-9838-490691600FDA}. You will use the RegistryKey class (in System.Win32 assembly) to create a reference to the local machine in registry. Then use the OpenSubKey method of the RegistryKey class to connect to the subkey that contains the IP address of the local computer. This method accepts two parameters. The first parameter is the string representing the path of the subkey and the second parameter is the read-only/writable mode. Its value can be false or true. If you provide true for the read-only/writable mode, the value of the registry of the specified subkey can is writable. Otherwise,
it is read-only.

Imports System.Management
Imports Microsoft.Win32
Module Module1

   Sub Main()
     'read IP
     Dim hostname As String
     'get host name
     hostname = System.Net.Dns.GetHostName()
     'show host name
     'get a list of IP addresses for the give host name
     Dim hostentry As System.Net.IPHostEntry =  System.Net.Dns.GetHostEntry(hostname)
     'show the ip address of your pc used to connect to the network
     For Each ip As System.Net.IPAddress In hostentry.AddressList
        If ip.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
        End If
     'set ip
     Dim ipstr As String = ""
     Dim regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
     'Open connection to the Sub Key that stores ip address of th PC
     Dim subKey As Microsoft.Win32.RegistryKey = regKey.OpenSubKey("SYSTEM\\ CurrentControlSet\\services\\ Tcpip\\Parameters
\\ Interfaces\\ {D21C6DAA-DAAC-4F79-9838-490691600FDA}", True)
     'Change ip address of the PC
     subKey.SetValue("IPAddress", ipstr)
     'close connection to sub key
      Console.WriteLine("The IP address of the PC has been changed to " & ipstr)
   End Sub

End Module


Posted by: Dara | post date: 02-18-2014 | Subject: VB.NET Programming Language

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.