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.
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#
To use the function you will do a call like this,
C#,
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;
}
To use the function you will do a call like this,
C#,
string country;
GetCountry("111.111.111.111",out country);
Comments
Post a Comment