views:

336

answers:

3

My GPRS modem has got a sim card. it can connect Web. Web service give it a ip number. i need it. like that: http://www.your-ip-address.com/

How can i do that C#?

+1  A: 

You can create a WebRequest to http://whatismyip.com/automation/n09230945.asp which houses only your IP address

Start here

Antony Koch
i want to make it with C#?
Phsika
@ykaratoprak: so go ahead. What's preventing you from "make it with C#"?
John Saunders
i need static ip : http://www.your-ip-address.com/ this site give me what i desire ip. i need how to do that C# code
Phsika
http://whatismyip.com/automation/n09230945.asp gives you your IP just the same as your-ip-address.com and the page I quote explains how to create a web request with timeouts etc. as per your requirements
Antony Koch
+4  A: 

You can use the static method WebClient.DownloadString(url) to read your external IP address from any web service providing such data:

string ip = System.Net.WebClient.DownloadString("http://whatismyip.org/");

If you are going to use this in a production environment, better make sure that the URL you are pointing to, is guaranteed to stay around for the entire lifespan of your application. The best way is probably to host the web service yourself.

Also, you should add some error checking around this code, as it will fail if the internet connection or the web service is unavailable.

Jørn Schou-Rode
+1  A: 

You can get a list of your IP addresses via DNS using the following code:

var name = Dns.GetHostName();
var entry = Dns.GetHostEntry(name);
foreach (var address in entry.AddressList) {
   Console.WriteLine(address);
}

If you want the IP address as a property of the hardware, you can use the System.Management.ManagementClass with the name Win32_NetworkAdapterConfiguration. See http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx for details.

Brian Rasmussen
This code will return only *local* IP addresses, or am I mistaken? In many cases, that would not be the same as the *remote* address reported by `your-ip-address.com`
Jørn Schou-Rode
That depends. When I am using my dial-up, it uses PPP and my connection is assigned a public IP-adress. So the code above returns the same IP address as your-ip-address.com.
Brian Rasmussen