Click here to Skip to main content
15,900,461 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Whats My IP Address ?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
22 Sep 2011CPOL 9.5K   1   5
On Vista/Win 7, I have seen code where only a single period is returned for (0). This makes sure you get the actual IP regardless of the system.Const IPPattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"With Dns.GetHostEntry(System.Environment.MachineName) Return...

On Vista/Win 7, I have seen code where only a single period is returned for (0). This makes sure you get the actual IP regardless of the system.


VB
Const IPPattern As String = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"

With Dns.GetHostEntry(System.Environment.MachineName)
    Return .AddressList.Where(Function(a) _
       Regex.IsMatch(a.ToString, IPPattern)).First.ToString
End With

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect GBIS
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: sorry typoed public function not private. Pin
nlarson1128-Nov-11 10:21
nlarson1128-Nov-11 10:21 
GeneralRe: you need to turn "option infer" on in your project propertie... Pin
nlarson1128-Nov-11 10:20
nlarson1128-Nov-11 10:20 
GeneralThe IPPattern variable is a regular expression looking for ... Pin
nlarson1121-Nov-11 11:39
nlarson1121-Nov-11 11:39 
The IPPattern variable is a regular expression looking for 255.255.255.255 pattern

The “with” is a shortcut for property and method access of the object being “withed”.
So instead of typing
Textbox1.text = “”
Textbox1.tag = “”

You would type with infront
With textbox1
.text = “”
.tag = “”
End with

The return line is using linq looking for a regular expression match in all the instances of addresslist
Once found, it returns the first one that matches (.first) instead of going through all the matches.

Hope this helps.
Nathan
GeneralRe: Tried again but come up with a overload resolution failed be... Pin
ledtech328-Nov-11 9:46
ledtech328-Nov-11 9:46 
GeneralUm how does this code work,Iv'e created a new Project to tes... Pin
ledtech321-Nov-11 6:37
ledtech321-Nov-11 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.