0

I want to retrieve all country codes (like +61 for Australia) by giving country name. Is there built in method for retrieving country code in c# like

int countryCode = CultureInfo.getCountryCode("CountryName");

I saw some methods on internet but non of them is simple. I want simple method for retrieving country code by giving country name. I saw Google libphonenumber but is there any simple solution because I am having trouble while including thi library because I am using visual studio 2015 but this library is build in visual studio 2017. If this library is only the solution then how can I include this library in my visual studio 2015 project?

This is the code for getting all countries

public static List<string> countryList()
    {
        List<string> cultureList = new List<string>();
        CultureInfo[] info = getCultureInfo();

        foreach (CultureInfo culture in info)
        {
            RegionInfo region = new RegionInfo(culture.LCID);

            if (!(cultureList.Contains(region.EnglishName)))
            {
                cultureList.Add(region.EnglishName);
            }
        }

        cultureList.Sort();
        return cultureList;
    }

Now, how can I get country codes all of theses?

Hafiz Hamza
  • 311
  • 3
  • 16
  • Do you need fuzzy matching of country name? Or does your input consist of full come ntry names perfectly matching he names .net uses? – CodesInChaos Feb 03 '18 at 08:02
  • I want a method which accept country name and give the appropriate country code. I am getting country names from .net built in Class "CultureInfo" – Hafiz Hamza Feb 03 '18 at 08:05
  • Why are you doing this in the first place? Sounds like a xy problem candidate to me. – CodesInChaos Feb 03 '18 at 08:06
  • then what is the best solution @CodeInChaos – Hafiz Hamza Feb 03 '18 at 08:07
  • 1
    Where do you get the country names from? Probably not a free text field, right? – CodesInChaos Feb 03 '18 at 08:09
  • @CodesInChaos see updated question – Hafiz Hamza Feb 03 '18 at 08:22
  • A quick Google search for "country code phone api" returns [CountryCodes.json](https://gist.github.com/Goles/3196253) Gist with lots of lists of these codes. Whether you need them to be always up to date is another detail that you did not provide and I would suggest using an API solution instead of copy-paste if you do. – NightOwl888 Feb 03 '18 at 15:11
  • Possible duplicate of [List of phone number country codes](https://stackoverflow.com/questions/2530377/list-of-phone-number-country-codes) – NightOwl888 Feb 03 '18 at 15:13

1 Answers1

0

To answer your question, if you are talking about telephone prefix as country code, then there is no builtin way as I am aware of. Neither, culture nor region contains that information.

The easiest way is to take advantage of some free API, for example geognos, where you can get the json data of a country using the code you can get from culture/region. For example data for Australia would be found using the AU-code.

Edit.

To clarify my answer a bit, country code has nothing to do with tel. prefix and country codes at different formats can be fetched from culture/region information.

Niko
  • 444
  • 3
  • 9