Get Local IP Address Using WMI

evad4682

Well-known member
Joined
Nov 7, 2005
Messages
55
Programming Experience
Beginner
Hello Everybody

I am trying to find a way to get the ip address of the machine that my application is running on. I was attempting to do the following:
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] moOS [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ManagementObjectSearcher
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] moIP [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ManagementObject
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ip [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]moOS = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] moIP [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] moOS.Get
ip = moIP("IPAddress").ToString
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]lblIP.Text = ip
[/SIZE]

This is returning "System.String[]" to my label. What am I missing? Is there a better way to do this?

Thanks for the help in advance.
 
Try this.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] h [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)[/SIZE]
[SIZE=2]lblIP.Text = [SIZE=2]h.AddressList.GetValue(0).ToString[/SIZE]
[/SIZE]
 
Last edited:
Thanks a ton! That worked perfectly! I think I need to learn more about System.Net instead of wrestling with WMI.
:D
 
IPAddress property from that WMI class is an array of strings, so this should get an IP:
VB.NET:
[SIZE=2]ip = moIP([/SIZE][SIZE=2][COLOR=#800000]"IPAddress"[/COLOR][/SIZE][SIZE=2])(0)[/SIZE]
You can also limit the WMI query further if the IP address info is all you need:
VB.NET:
[SIZE=2][COLOR=#800000]SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True[/COLOR][/SIZE]

CPCisHeres code works also.
WMI is not a struggle if you got just a little knowledge about it and the WMI Code Creator 1.0 You can do lookups directly to documentation from that tool too.
 
Thank you very much CPCisHERE and JohnH, your help has been much appreciated.

JohnH, When I was using WMI to get an ip address I was returning "System.String[]". Did I need to specify some how which network adapter I was trying to get an ip address from? Maybe I am just not understanding how to reference the array of strings that the WMI class returns for IPAddress. Quite more, I may just need to educate myself more on WMI and System.Net. Thank you for the help, forums like these make it much easier to learn this stuff.
 
Yes, you were getting a string array for each moIP (network adapter), and needed to check all (for each string) or one local ip using the array qualifier like the example moIP("IPAddress")(0) <- which means the first array element in IPAddress property string array of moIP adapter.
 
Back
Top