How to get my IP address?

  • Thread starter Thread starter Bob Altman
  • Start date Start date
B

Bob Altman

Hi all,

Here's a (hopefully) simple question: How do I get a string containing my IP address (e.g., "129.200.123.456")? TIA!
 
Hi,
U can retreive IP address from server variables.
Request.Servervariable("REMOTE_ADDR")

SHrawan
-----Original Message-----
Hi all,

Here's a (hopefully) simple question: How do I get a
string containing my IP address
(e.g., "129.200.123.456")? TIA!
 
Whoops... sorry, I guess no question is ever as simple as it seems. My application is a Windows Forms application, not a web app.
My question should more properly have been:

How can my Windows Forms app get a string containing my IP address (e.g., "129.200.123.456")

TIA!
 
Hi,

Try this

using System.Net;

System.Net.IPAddress addr;
addr = new System.Net.IPAddress(Dns.GetHostByName
(Dns.GetHostName()).AddressList[0].Address);

addr.ToString() is an IP address.

Hope this will help U

Regards
-----Original Message-----
Whoops... sorry, I guess no question is ever as simple as
it seems. My application is a Windows Forms application,
not a web app.
My question should more properly have been:

How can my Windows Forms app get a string containing my
IP address (e.g., "129.200.123.456")
 
shrawan said:
Hi,

Try this

using System.Net;

System.Net.IPAddress addr;
addr = new System.Net.IPAddress(Dns.GetHostByName
(Dns.GetHostName()).AddressList[0].Address);

addr.ToString() is an IP address.

Keep in mind that this only gets you one of the machine's IP addresses.
 
Thanks for the code sample. It gives the correct result, but I get a compilation warning to the effect that the Address() property
is obsolete because it is address family dependent. Is there a way to do this without using the Address property?

shrawan said:
Hi,

Try this

using System.Net;

System.Net.IPAddress addr;
addr = new System.Net.IPAddress(Dns.GetHostByName
(Dns.GetHostName()).AddressList[0].Address);

addr.ToString() is an IP address.

Hope this will help U

Regards
-----Original Message-----
Whoops... sorry, I guess no question is ever as simple as
it seems. My application is a Windows Forms application,
not a web app.
My question should more properly have been:

How can my Windows Forms app get a string containing my
IP address (e.g., "129.200.123.456")
 
It turns out that this simpler expression returns the IP dot address without referencing the obsolete Address property:

Dns.GetHostByName(Dns.GetHostName).AddressList(0).ToString



Thanks again for the help!

Bob Altman said:
Thanks for the code sample. It gives the correct result, but I get a compilation warning to the effect that the Address() property
is obsolete because it is address family dependent. Is there a way to do this without using the Address property?

shrawan said:
Hi,

Try this

using System.Net;

System.Net.IPAddress addr;
addr = new System.Net.IPAddress(Dns.GetHostByName
(Dns.GetHostName()).AddressList[0].Address);

addr.ToString() is an IP address.

Hope this will help U

Regards
-----Original Message-----
Whoops... sorry, I guess no question is ever as simple as
it seems. My application is a Windows Forms application,
not a web app.
My question should more properly have been:

How can my Windows Forms app get a string containing my
IP address (e.g., "129.200.123.456")
TIA!

Hi,
U can retreive IP address from server variables.
Request.Servervariable("REMOTE_ADDR")

SHrawan

-----Original Message-----
Hi all,

Here's a (hopefully) simple question: How do I get a
string containing my IP address
(e.g., "129.200.123.456")? TIA!


.
 
Bob Altman said:
It turns out that this simpler expression returns the IP dot address
without referencing the obsolete Address property:
Dns.GetHostByName(Dns.GetHostName).AddressList(0).ToString

Bob, are you aware that "address family dependant" means that it could be an
IPv6 address? In that case, the result of .ToString will be a much longer
string with hexadecimal digits and colons instead of decimal digits and
dots.
 
John,

I guess that makes perfect sense. I'm looking to get the string form of the numeric IP address so that I can give it to other
computers in my network so that they can talk to me. If we're using IPv6 then I would expect to give them a string in IPv6 format.

Thanks everyone for the help!

- Bob
 
Back
Top