none
Getting the user's location (country) in C#? RRS feed

  • Question

  • How can I find the user's (client computer's) location (country) in a C# Windows Application? I want to get their country and filter the items in a combobox to the determined country as default.

    I have the comboboxes set up corretly in Visual C# Beta 1 are getting the cities from my database. Basically the first combobox is full of Countries and the second is full of cities (filtered for the selected country).

    Best Regards,

    Dean Krause
    Thursday, April 14, 2005 5:58 AM

Answers

  • You can use the following:

    using System.Globalization;
    ...
    // returns "language (country)" e.g. English (Canada)
    string culture = CultureInfo.CurrentCulture.EnglishName;
    string country = culture.Substring(culture.IndexOf('(') + 1, culture.LastIndexOf(')') - culture.IndexOf('(')-1);   // You could also use a regex, of course

    If you need the country name in the native language of that country, you can use CultureInfo.CurrentCulture.NativeName instead. If you need the two letter country code, you can use CultureInfo.CurrentCulture.Name, which returns "languageCode-countryCode" (e.g. en-CA).
    Thursday, April 14, 2005 6:34 AM

All replies

  • You can use the following:

    using System.Globalization;
    ...
    // returns "language (country)" e.g. English (Canada)
    string culture = CultureInfo.CurrentCulture.EnglishName;
    string country = culture.Substring(culture.IndexOf('(') + 1, culture.LastIndexOf(')') - culture.IndexOf('(')-1);   // You could also use a regex, of course

    If you need the country name in the native language of that country, you can use CultureInfo.CurrentCulture.NativeName instead. If you need the two letter country code, you can use CultureInfo.CurrentCulture.Name, which returns "languageCode-countryCode" (e.g. en-CA).
    Thursday, April 14, 2005 6:34 AM
  • Actually a more straightforward way to get the current country (as set in Control Panel... Regional and Language Options) is to examine System.Globalization.RegionInfo.CurrentRegion.EnglishName or System.Globalization.RegionInfo.CurrentRegion.DisplayName (localized country name). No need to parse it out of CultureInfo as mentioned in my previous post.

    Friday, April 15, 2005 5:48 AM
  • Thanks a lot for the help, works great Smile

    Where could I find a list of all the possible returns?? (e.g. Australia.....).
    Friday, April 15, 2005 7:10 AM
  •  Dean Krause wrote:
    Thanks a lot for the help, works great Smile

    Where could I find a list of all the possible returns?? (e.g. Australia.....).


    Dean,

       It should be noted that while this is a very good guess, it is still a guess none-the-less.  It is only the users culture, not the actual location.  It's usually a good guess (I used en-US, and I am in the US), but it will not work for everyone.

       Hope this helps.

          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com
    Friday, April 15, 2005 3:19 PM
  • If reading the Region Information from the Settings is enough that would do.

    But what would you do if the user changes the settings to some other country..


    RIL can be used to detect the CellID, Mobile Country Code, Location Area Code, etc.


    There is an RIL Wrapper here


    I tried it and it could identify my country.


    If you want a list of Mobile Country Codes for all the countries, you can get it here



    Akash M Thambiran
    Friday, August 21, 2009 11:38 AM
  • In real life this not a good option. Lof of PC's are just put in English suggesting United States.

    Guess the only reliable option is via IP adress when system is on the internet ?

    Thursday, August 15, 2013 3:57 PM