Answered by:
Getting the user's location (country) in C#?

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
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).
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). -
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.
-
-
The list of possible values is available in the MSDN docs:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationregioninfoclasstopic.asp -
Dean Krause wrote: Thanks a lot for the help, works great
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 -
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- Proposed as answer by Akash M Thambiran Friday, August 21, 2009 11:38 AM
-