Tuesday, July 2, 2013

Get a List of all Countries

This is a very common requirement that comes up to me in every alternate application that I make. I know there would be a lot of people like me who have the same issue.

At first I thought of consuming a ready made webservice that would give me the list of all countries.

Then upon further research, I found a very simple yet effective solution to my problem:

Here is a function that returns a List of Country Names.

public static List GetCountryList()
        {
            List _CultureList = new List();
            CultureInfo[] _CultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo _Culture in _CultureInfo)
            {
                RegionInfo _RegionInfo = new RegionInfo(_Culture.LCID);
                if (!(_CultureList.Contains(_RegionInfo.EnglishName)))
                {
                    _CultureList.Add(_RegionInfo.EnglishName);
                }
            }
            _CultureList.Sort();
            return _CultureList;
        }

No comments:

Post a Comment