Click here to Skip to main content
15,909,651 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How can I get my public IP address through C# code?
Posted
Updated 8-Nov-10 23:20pm
v4

1 solution

Hi,

You will need to ask another server to tell you your public address. Maybe this will help you:

public string getPublicIP() 
{ 
    string direction; 
    WebRequest request = WebRequest.Create("http://checkip.dyndns.org/"); 
    WebResponse response = request.GetResponse(); 
    StreamReader stream = new StreamReader(response.GetResponseStream()); 
    direction = stream.ReadToEnd(); 
    stream.Close(); 
    response.Close(); 
 
    //Search for the ip in the html 
    int first = direction.IndexOf("Address: ") + 9; 
    int last = direction.LastIndexOf("</body>"); 
    direction = direction.Substring(first, last - first); 
 
    return direction; 
} 
 
Share this answer
 
Comments
abdul kamaal naser 9-Nov-10 6:33am    
thanks a lot

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900