用C#获取一个国家的洲

发布于 2024-10-26 17:40:42 字数 100 浏览 8 评论 0原文

有没有办法在 C# 中使用 RegionInfo 类或任何其他方式查找国家/地区的大陆?

例如,您给出国家/地区名称“丹麦”,它会返回“欧洲”。

Is there a way to find a country's continent in C#, using the RegionInfo class or any other way?

For example, you give the country name "Denmark" and it returns "Europe".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

愿与i 2024-11-02 17:40:42

.NET 框架中没有类似的情况。它对欧亚大陆和大洋洲的地理定义也相当模糊。目前使用的有 6 种不同的定义。您需要自己的数据库。

Nothing like that in the .NET framework. It is also rather ambiguous with geographical definitions for Eurasia and Oceania. There are 6 distinct definitions in use. You'll need your own dbase.

身边 2024-11-02 17:40:42

您可以存储哈希表。这些条目将以国家/地区名称作为键,以大陆名称作为值。
只需从一些公共数据库生成此表,一旦获得它,您就可以简单地查询它。它也相当快。

http://www.geonames.org/countries/
您可以从那里获取数据库

You can store a hash table. The entries will have as key the country name and as value the continent name.
Just generate this table from some public database and once you have it you can simply query it. It's also pretty fast.

http://www.geonames.org/countries/
You can get from there a database

尐籹人 2024-11-02 17:40:42

只有几大洲和几百个国家,因此构建自己的桌子并不困难。对于大陆到底是什么(澳大利亚/大洋洲/澳大利亚/亚洲有时是一个特别棘手的大陆),以及土耳其和俄罗斯是否属于欧洲、亚洲或两者都存在意见分歧。尽管如此,自己动手至少可以让你在这些问题上做出自己的决定。

There are only a few continents and only a couple of hundred countries, so it wouldn't be very hard just to build your own table. There are differences of opinion as to just what the continents are (Australia/Oceania/Australasia/Asia being a particularly prickly one at times), and whether e.g. Turkey and Russia are in Europe, Asia, or both. Still, rolling your own at least lets you make your own call on these matters.

要走干脆点 2024-11-02 17:40:42

我知道这是一个老问题,但我已经为 C# 创建了一个库,其中包含内容信息。您可以在这里找到我的项目 Nager.Country 我希望它对您有所帮助。

nuget 包也可用 install-package Nager.Country

示例

ICountryProvider countryProvider = new CountryProvider();
var countryInfo = countryProvider.GetCountry(Alpha2Code.DK);

I know it's an old question but i have create a library for C# and this contains the contintent information. You can found my project here Nager.Country I hope it helps you.

A nuget package is also available install-package Nager.Country

Example

ICountryProvider countryProvider = new CountryProvider();
var countryInfo = countryProvider.GetCountry(Alpha2Code.DK);
━╋う一瞬間旳綻放 2024-11-02 17:40:42

RegionInfo 不存储大陆。但您可以使用 GeoNames.org 国家/地区信息 构建数据库。数据格式为 utf8 编码的制表符分隔文本。它包含您需要的所有信息以及更多信息。

RegionInfo doesn't store continent. But you can build a database using GeoNames.org Country Information. The data format is tab-delimited text in utf8 encoding. It's has all the information and more that you need.

清旖 2024-11-02 17:40:42

您可以根据您需要的大陆模型构建自己的查找列表。
例如,在美国和英国,我们将美国视为两个独立的大陆(北美和南美),而大多数其他国家的人们都被告知美国是一个大陆。

您还应该考虑到一些国家位于两大洲。俄罗斯的一部分在欧洲,其余部分在亚洲。土耳其也一样。

You can build your own lookup lists according to the continent model you need.
For example, in the US and the UK we consider America as two separate continents (North America and South America), whereas people in most other countries are taught that America is a single continent.

You should also consider that some countries lay in two continents. A part of Russia is in Europe, the rest is in Asia. Same with Turkey.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文