Agha Usman

Lives in Karachi (Pakistan) and work for Ciber Strategies

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.

Posted: Jul 26 2009, 07:37 PM by aghausman12 | with 7 comment(s)
Filed under: ,

Comments

Get Countries Name in .Net - Agha Usman said:

Pingback from  Get Countries Name in .Net - Agha Usman

# July 26, 2009 10:07 AM

Get Countries Name in .Net | I love .NET! said:

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

# July 26, 2009 11:43 AM

Get Countries Name in .Net | rapid-DEV.net said:

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

# July 26, 2009 12:28 PM

MrTumi said:

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

# July 27, 2009 11:18 AM

Nam said:

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

# July 27, 2009 1:49 PM

webbes said:

Nice one! Never thought of it.

# July 27, 2009 5:37 PM

puru said:

Great article...

# August 4, 2009 8:11 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)