2017-06-01 74 views
1

我想根据用户的当前区域下载网站的内容。为此,我需要像US,FR等国家代码。如何获得该?如何在Windows 10 UWP上获取用户区域?

我尝试这些:

这个总是显示美国。我认为它来自应用程序清单。

var region = RegionInfo.CurrentRegion; 

这:

var cultureName = new DateTimeFormatter("longdate", new[] { "US" }).ResolvedLanguage; 

return new CultureInfo(cultureName); 

这个工程。返回类似en-US的东西,我可以从中提取国家代码。但有时它可能仅返回语言en而没有国家代码。

如何获取系统当前的两个字符的国家代码?谢谢。

+0

请记住,很多人在设备设置上设置美国,但他们不在美国境内 – Quincy

+0

@Quincy yes我知道这一点。这对我的案例,他们的选择不是问题。 – Blendester

回答

2

要获取用户的家庭区域设置,我们可以使用GlobalizationPreferences.HomeGeographicRegion属性。

// Obtain the user's home geographic region. 
var region = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion; 

归属区域通常是在安装Windows时设定,也是
设置→时间&语言版本→区&语言→国家或地区

Windows.Globalization也有一个GeographicRegion作为帮助对象提供的对象。它允许应用程序检查特定区域的详细信息,例如显示名称,本地名称和正在使用的货币。

// Get the user's geographic region and its two-letter identifier for this region. 
var geographicRegion = new Windows.Globalization.GeographicRegion(); 
var code = geographicRegion.CodeTwoLetter; 

欲了解更多信息,请参阅Manage language and region

+0

非常感谢你..非常有用 – Blendester

相关问题