Determine your computer's IP address with VB.NET
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!
Print/View all Posts Comments on this article
|
|
White Papers, Webcasts, and Downloads
- New College Durham Attracts New Students With Robust IT Infrastructure Delivered With the Help of Dell Professional Services Dell When New College Durham received funding from the Learning Skills Council ... Download Now
- Dell's IT Infrastructure Services, Desktops, and Notebooks Allow Global Consumer Packaged Goods Marketer Unilever to Support Staff Efficiency and Productivity With Business-Critical IT Services Dell Unilever is a multinational corporation that owns 400 consumer brands ... Download Now
- Wireless Lab Delivers Technology and a Better Education Dell Steve Spangler, assistant principal of Middlesboro Middle School in ... Download Now
- Leveraging SMB ERP for an Economic Recovery ZDNet Times are tough but better days are sure to follow. In the wake of an ... Download Now
- Networking and Unified Communications CDW The flourishing area of unified communications offers further testimony to ... Download Now
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

