本文介绍了使用C#获取当前的国家和位置详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的应用程序在哪里使用。

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.

有没有办法读取任何OS设置/系统设置并获取最新信息?

Is there any way to read any OS settings/ System settings and get the current country where my application is being used?

推荐答案

您可以使用IpInfo通过用户的Internet地址获取其国家/地区。
,除非他们在VPN或代理下,这是最好的选择。

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; }
    }
}

请注意必须使用4.5或更高版本的净框架能够将json转换为没有第三方库的对象。

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.

这篇关于使用C#获取当前的国家和位置详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:16