how to display my IP address?

C

cj

I can get my DNS Name, Machine Name and User Name like

MessageBox.Show("NetBIOS Name: " & System.Environment.MachineName &
vbCrLf & _
"DNS Name: " & System.Net.Dns.GetHostByName("LocalHost").HostName &
vbCrLf & _
"User Name: " & System.Environment.UserName)

how can I display my IP address?

Also, what is the difference between a DNS Name and a NetBIOS name?
 
S

Sam Malone

Public Function Get_LocalIPAddress() As String

Dim h As System.Net.IPHostEntry =
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)

'return any string found

Get_LocalIPAddress = CType(h.AddressList.GetValue(0),
System.Net.IPAddress).ToString

End Function
 
P

Peter

Sam,

Maybe we should use System.Net.Dns.Resolve(System.Net.Dns.GetHostName)
instead of
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).

Peter
 
S

Sam Malone

Go for it and I'd appreciate hearing which is better (if indeed one is
better than the other)
 
C

cj

I don't get an IP address. That gives me:

? System.Net.Dns.Resolve(System.Net.Dns.GetHostName)
{System.Net.IPHostEntry}
AddressList: {Length=1}
Aliases: {Length=0}
HostName: "CJ"

I'm using VS2003 if that changes things. I should have mentioned it.
 
J

Jeffrey Tan[MSFT]

Hi cj,

Thanks for your feedback!

You can get the IP address like this:
private void button1_Click(object sender, System.EventArgs e)
{
IPHostEntry hostInfo = Dns.GetHostByName("host name");
byte[] ipaddr=hostInfo.AddressList[0].GetAddressBytes();
MessageBox.Show("IP Address:
"+ipaddr[0]+"."+ipaddr[1]+"."+ipaddr[2]+"."+ipaddr[3]+"\n");
}

This code snippet works well on my side. Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cor Ligthert [MVP]

Jeffrey,

I had to smile, but it is true, be aware that this is a VB Net newsgroup and
that your code can look confusing for some people searching this newsgroup.

Cor
 
J

Jeffrey Tan[MSFT]

Oh, yes, it seems that I mislooked the queue name :-(

Anyway, a C# to VB.net converter can help here:
http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

cj

The converter doesn't work exactly but close enough. Because I want to
know the ip address of the machine this is running on and don't want to
have to hard code the computers name in the code I changed the dim
hostinfo line as shown below. Thanks for the code.

Dim hostInfo As System.Net.IPHostEntry = _
System.Net.Dns.GetHostByName(System.Net.Dns.GetHostByName("LocalHost").HostName)

Dim ipaddr As Byte() = hostInfo.AddressList(0).GetAddressBytes

MessageBox.Show("IP Address:" & ipaddr(0) & "." & ipaddr(1) & "." & _
ipaddr(2) & "." & ipaddr(3) & "" & Microsoft.VisualBasic.Chr(10) & "")
 
J

Jeffrey Tan[MSFT]

Hi cj,

I am glad you have figured out what you need. If you need further help,
please feel free to post. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top