问题
I want to know where my application is used.
Here is the code for getting the country name and Time zone :
TimeZone localZone = TimeZone.CurrentTimeZone;
var result = localZone.StandardName;
var s = result.Split(' ');
Console.WriteLine(s[0]);
Console.WriteLine(RegionInfo.CurrentRegion.DisplayName);
But my issue is, any one can change the time zone. Based on the time zone I may get wrong name. And the region settings is used as united states which cannot be changed. Because all the users has same settings and there are hundreds of thousands of users to my application.
Is there any way to read any OS settings/ System settings and get the current country where my application is being used?
回答1:
You can use IP address to get location, there are many options to do it, and one of them is ipinfodb, you can get example from here - Class CountryIP VB/C#
回答2:
You can use IpInfo to get a user's country by their internet address. unless they're under VPN or Proxy this is your best bet.
class Program
{
static void Main(string[] args)
{
GetCountryByIP();
}
public static void GetCountryByIP()
{
IpInfo ipInfo = new IpInfo();
string info = new WebClient().DownloadString("http://ipinfo.io");
JavaScriptSerializer jsonObject = new JavaScriptSerializer();
ipInfo = jsonObject.Deserialize<IpInfo>(info);
RegionInfo region = new RegionInfo(ipInfo.Country);
Console.WriteLine(region.EnglishName);
Console.ReadLine();
}
public class IpInfo
{
//country
public string Country { get; set; }
}
}
Notice net framework 4.5 or above is required to be able to convert json to object without 3rd party libraries.
if you target lower frameworks, you can parse the info string for yourself.
来源:https://stackoverflow.com/questions/41996028/get-current-country-and-location-details-using-c-sharp