IPAddress To Country Resolver


Posted Date:     Total Responses: 0    Posted By: ABitSmart   Member Level: Gold   Points/Cash: 100   


There are situations where we have to track visitors. The tracking might include the country from the visitor has browsed the website. These statistics are often important.

This project provides a library to resolve the country name given an IP. The project uses the Codeplex IPExtensions projects. This project has embedded information of the various ip address ranges assigned to different countries.

The major advantage of this method is a single DLL. It does not require any database setup etc. You just need to add reference to the IpToCountryLib DLL provided in the solution.

To resolve the country name, the library API to be used is "GetCountry". GetCountry takes in a string parameter which is the IP Address and returns the country name as the out parameter.

This is the code of the function,
C#

public bool GetCountry(string userHostIpAddress, out string countryName)
{
bool result = false;
countryName = string.Empty;

if (string.IsNullOrEmpty(userHostIpAddress))
return false;

IPAddress ipAddress;
if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
{
countryName = ipAddress.Country();

result = true;
}

return result;
}

VB,

Public Function GetCounTry(ByVal userHostIpAddress As String, ByRef counTryName As String) As Boolean
Dim result As Boolean = False
counTryName = String.Empty

If String.IsNullOrEmpty(userHostIpAddress) Then
Return False
End If

Dim ipAddress As IPAddress
If IPAddress.TryParse(userHostIpAddress,out ipAddress) Then
counTryName = ipAddress.CounTry()

result = True
End If

Return result
End Function


To use the function you will do a call like this,
C#,

IPToCountry _lib = new IPToCountry();
string country;
_lib.GetCountry("111.111.111.111",out country);

VB.Net,

Dim _lib As IPToCounTry = New IPToCounTry()
Dim counTry As String
_lib.GetCounTry("111.111.111.111",out counTry)


I have attached the source code and also an exe for the application. The application attached contains a WPF application using the library and demonstrating the feature.

Have fun.

Attachments

  • Source Code (663-262226-IPToCountry.rar)
  • Exe (663-262226-EXE.rar)

  • Project Feedbacks

    Author: Member Level: GoldRevenue Score: 1 out of 5
    hey really nice code. do you have any code to get domain from where we reached the current page.


    Author: Member Level: SilverRevenue Score: 1 out of 5
    how can i implement this in my application...i tried am not getting anything.


    Author: Member Level: GoldRevenue Score: 1 out of 5
    Indeed a very good article and effective as I needed it on my project.

    Thanks


    Author: Member Level: BronzeRevenue Score: 2 out of 52 out of 5
    hi,
    very nice to post such a project. this's a most wanted snippet needed by many serious developers.


    Author: Member Level: SilverRevenue Score: 1 out of 5
    hi,
    nice project.
    how we run this project?
    how we use it in our application?




    Author: Member Level: GoldRevenue Score: 2 out of 52 out of 5
    Hi,
    Its really nice. I would like learn such type of things.

    It will be greatful for me to send the files.

    Thanks
    Siva Sreenath


    Post Feedback
    You must Sign In to post a feedback.
    Next Project: Bug Tracking System
    Previous Project: Training And Placement

    Return to Project Index

    Post New Project


    Related Projects


    Top Contributors
    Today
      Last 7 Days
        more...

        Awards & Gifts

        Online Members

        More...
         
        Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India