Get Countries Name in .Net

Introduction:

In this post, I will explain you how can we get the countries name filled in any collection using .net without using any database.

It is a regular task, which we all as developers did some past day but the difference is we used database table or xml file to hold the country names. But .net framework provide us with all the countries information in Globalization namespace.

So, here is the code for that

Dictionary<string,string> objDic = new Dictionary<string,string>();
 
foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
    RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);
    if (!objDic.ContainsKey(objRegionInfo.EnglishName))
    {
        objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower());
    }
}
 
var obj = objDic.OrderBy(p => p.Key );
foreach (KeyValuePair<string,string> val in obj)
{
    ddlCountries.Items.Add(new ListItem(val.Key, val.Value));
}

 

Explanation:

Notice that, we have used typed dictionary object to store the name and the values of the countries.

Then, we use CultureInfo.GetCultures to get the cultural information of the countries.

Later on, we use RegionInfo to get the regional information of that  culture.

Since, there can be multiple cultures of the same country that is why there is a condition which check either the country is already added in dictionary. If not, then simply add the country name and country two letter name. (Note : We are treating the two letter country name as the value)

After the loop, I used some LinQ stuff to sort county names, and then iterate through the returned object to add the values in drop down list.

That’s it. Now you are not only limited to show the English name of the country but you can also show the native name. For example, the name of my country in English is “Islamic Republic of Pakistan” but the native name is پاکستان.

Also, you can get the following country information using RegionInfo

 

sc_clbn_1

Some developers are habitual of using country id along with the country name. if they still want to use some id to save the country information they can use the GeoId property of the RegionInfo.

Published Sunday, July 26, 2009 7:37 PM by aghausman12
Filed under: ,

Comments

# Get Countries Name in .Net - Agha Usman

Sunday, July 26, 2009 10:07 AM by Get Countries Name in .Net - Agha Usman

Pingback from  Get Countries Name in .Net - Agha Usman

# Get Countries Name in .Net | I love .NET!

Sunday, July 26, 2009 11:43 AM by Get Countries Name in .Net | I love .NET!

Pingback from  Get Countries Name in .Net | I love .NET!

# Get Countries Name in .Net | rapid-DEV.net

Sunday, July 26, 2009 12:28 PM by Get Countries Name in .Net | rapid-DEV.net

Pingback from  Get Countries Name in .Net | rapid-DEV.net

# re: Get Countries Name in .Net

Monday, July 27, 2009 11:18 AM by MrTumi

Great post! Your topic helps me much!  Thank you!

# re: Get Countries Name in .Net

Monday, July 27, 2009 1:49 PM by Nam

Very good tips for getting the Country's name without using external Xml file or Database. Thank you.

# re: Get Countries Name in .Net

Monday, July 27, 2009 5:37 PM by webbes

Nice one! Never thought of it.

# re: Get Countries Name in .Net

Tuesday, August 04, 2009 8:11 AM by puru

Great article...

Leave a Comment

(required) 
(required) 
(optional)
(required)