On ZDNet: Can Bing be your search engine?

Determine your computer's IP address with VB.NET

Tags: Microsoft development tools, Microsoft Visual Basic.Net, computer, Irina Medvinskaya, host name, IP address, IP, GetHostByName, Visual Basic Tips Newsletter

  • Save
  • Print
  • Recommend
  • 9

Takeaway: Did you know that you can use VB.NET's System.Net.Dns class to find out your computer's IP address? Irina Medvinskaya offers two code samples that show how this is possible.

There may be times when you need to know the IP address of your machine, such as when you're writing some networking code. This tip shows how you can figure out the IP address of your current computer using VB.NET's System.Net.Dns class.

System.Net.Dns class

This class offers a simple way of determining the IP address of a machine by utilizing its methods GetHostName and GetHostByName. The class also offers domain name resolution services. Its GetHostName method allows you to find out the host name of the current machine. You can use the GetHostByName method to determine the IP address of the machine.

I'll present two examples in which I achieve the same functionality in two slightly different ways, while the end result for both is exactly the same. (In the second example, I also utilize a Resolve method of the same class.)

Method 1

In Listing A, I determine the host name of the current machine by utilizing the GetHostName method of the System.Net.Dns class. Then, I use this host name as a parameter necessary to determine the IP address of the current computer. For that I utilize the GetHostByName method of the same class and pass to it the host name of the machine. Since the GetHostByName method returns a list of usable IP addresses, I only pull information about the first item in the array by specifying AddressList(0) and then converting the whole expression to string. After that, I display both the host name and the IP address in a message box.

Method 2

In Listing B, I determine the host name of the current machine and then use the Resolve method of the same class, passing to it the host name and pulling the information only about the first item in the Addresslist and converting the expression into a string. The result is displayed in a message box.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

  • Save
  • Print
  • Recommend
  • 9

Print/View all Posts Comments on this article

System.Net.Dns.GetHostByName(string) is obsolete just_chilin | 09/29/06

What do you think?

White Papers, Webcasts, and Downloads

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

Smartphones

advertisement
Click Here