private string LookupCountry() { string IP; bool IsLocal = false; bool _CacheGeoIPData = true; string _GeoIPFile; _GeoIPFile = "controls/CountryListBox/Data/GeoIP.dat"; if (this.Page.Request.UserHostAddress == "127.0.0.1") { // 'The country cannot be detected because the user is local. IsLocal = true; // Set the IP address in case they didn't specify LocalhostCountryCode IP = this.Page.Request.UserHostAddress; } else { // Set the IP address so we can find the country IP = this.Page.Request.UserHostAddress; } // Check to see if we need to generate the Cache for the GeoIPData file if (this.Context.Cache.Get("GeoIPData") == null && _CacheGeoIPData) { // Store it as well as setting a dependency on the file this.Context.Cache.Insert("GeoIPData", CountryLookup.FileToMemory(this.Context.Server.MapPath(_GeoIPFile)), new CacheDependency(this.Context.Server.MapPath(_GeoIPFile))); } // Check to see if the request is a localhost request // and see if the LocalhostCountryCode is specified if (IsLocal) { return(Null.NullString); } // Either this is a remote request or it is a local // request with no LocalhostCountryCode specified CountryLookup _CountryLookup; // Check to see if we are using the Cached // version of the GeoIPData file if (_CacheGeoIPData) { // Yes, get it from cache _CountryLookup = new CountryLookup((MemoryStream)this.Context.Cache.Get("GeoIPData")); } else { // No, get it from file _CountryLookup = new CountryLookup(this.Context.Server.MapPath(_GeoIPFile)); } // Get the country code based on the IP address string country = Null.NullString; try { country = _CountryLookup.LookupCountryName(IP); } catch (Exception ex) { Exceptions.LogException(ex); } return(country); }