by Viper
6. March 2009 06:05
Some time back I wrote an article on similar topic How to get IP address of a machine using C#. That article was written in days of .Net1.1. Since then .Net framework has come a long way. Some APIs were deprecated and a lot of new APIs were added. This post is an update on that old article. While I was working on a silverlight socket programming project, I had to deal with lot of Networking APIs. During that process I ended up writing a new method that returns me list of local IP addresses and presenting that to user to decide on what IP address they want to run socket server. Hope this post provides a quick coding tip on how you can get local IP address of your machine.
Private Function GetIPs() As StringCollection
Dim localIP = New StringCollection()
Dim localHostName = Dns.GetHostName()
Dim hostEntry = Dns.GetHostEntry(localHostName)
' Grab all ip addesses.
For Each ipAddr As IPAddress In hostEntry.AddressList
localIP.Add(ipAddr.ToString())
Next
GetIPs = localIP
End Function
Pardon my VB.Net code. I am not very intimate with this language. You can find C# code at old article location