locked
C# - How to programmatically get the location information which is set in the Region and Language->Location RRS feed

  • Question

  • Hi there,

    Is there any way to get the current location which is set in the Control Panel->Region and Languages->Location?

    I have tried to use the System.Globalization.RegionInfo.CurrentRegion but failed.

    Thanks for your help,

    Diego

    Thursday, February 24, 2011 5:22 AM

Answers

All replies

  • Did you try this?

     String Region = System.Globalization.RegionInfo.CurrentRegion.DisplayName;
     String Language = System.Globalization.CultureInfo.CurrentUICulture.EnglishName;
    

    Note it's the information for the currently logged in user.

    There's a lot of info in those structures, you may have to root around a while.


    Steve Wellens
    Thursday, February 24, 2011 10:55 PM
  • Steve,

    Thanks for the information. The way you suggested can only get the LCID of formats.

    What I meant was that I can't get the location information and I believe you misunderstood the question.

    Here is the screenshot of the location:

    http://www.vistax64.com/attachments/general-discussion/16648-os-language-location.jpg

     

    Thanks,

    Diego

    Friday, February 25, 2011 4:56 AM
  • Hi Diego,

     

    Welcome to the MSDN forum!

     

    Actually, Steve has shown a way to get the location name like” United States”.

    So what do you mean “the location information”? What is the actual result you want to get?


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 25, 2011 7:19 AM
  • Paul,

    What I'm talking about is the location, not the LCID set in the formats tab.

    If you go to the Control Panel->Region and Languages, in the "Region and Language" dialog, you will see there are several tabs (assuming you're using Windows 7).

    • The first tab is "Formats". - Steve's way can only get the LCID in this tab.
    • The second tab is "Location". - This is what I'm trying to get.

    Hope I have clarified my question.

    Thanks,

    Diego

    Friday, February 25, 2011 7:28 AM
  • Yes, I saw the tab “location”, but only show “United States”, and anything else?


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 25, 2011 7:34 AM
  • Hi Diego,

     

    You can get CurrentRegion:

    RegionInfo LocalReg = System.Globalization.RegionInfo.CurrentRegion;

    Then get some information from RegionInfo Properties.

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Thursday, March 3, 2011 3:12 AM
    Tuesday, March 1, 2011 4:38 AM
  • Hi Diego,

     

    You can get CurrentRegion:

    RegionInfo LocalReg = System.Globalization.RegionInfo.CurrentRegion;

    Then get some information from RegionInfo Properties.

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Hi Paul,

    Thanks for the help. You still don't get my question.

    If you follow steps mentioned below, you will see what's going on here.

    • Go to the Control Panel->Region and Languages dialog
    • In the the first tab  "Formats", set the format to "French (France)".
    • In the second tab "Location", set the location to "United States".
    • watch this property:  System.Globalization.RegionInfo.CurrentRegion.DisplayName

     

    The expected result would be "United States" but the actual result is "France".

     

     

    Thanks,

    Diego

    Monday, March 7, 2011 9:03 AM
  • Hi Paul,

    Thanks for the help. You still don't get my question.

    If you follow steps mentioned below, you will see what's going on here.

    • Go to the Control Panel->Region and Languages dialog
    • In the the first tab  "Formats", set the format to "French (France)".
    • In the second tab "Location", set the location to "United States".
    • watch this property:  System.Globalization.RegionInfo.CurrentRegion.DisplayName

     

    The expected result would be "United States" but the actual result is "France".

    Monday, March 7, 2011 9:04 AM
  • On my system (Windows 7, 64-bit), this works:

    using System.Runtime.InteropServices;
    using System.Text;
    
    private const int GEOCLASS_NATION = 16;
    
    //SYSGEOTYPE
    private const int GEO_NATION = 1;
    private const int GEO_LATITUDE = 2;
    private const int GEO_LONGITUDE = 3;
    private const int GEO_ISO2 = 4;
    private const int GEO_ISO3 = 5;
    private const int GEO_RFC1766 = 6;
    private const int GEO_LCID = 7;
    private const int GEO_FRIENDLYNAME = 8;
    private const int GEO_OFFICIALNAME = 9;
    private const int GEO_TIMEZONES = 10;
    private const int GEO_OFFICIALLANGUAGES = 11;
    
    
    [DllImport("kernel32.dll")]
    static extern int GetUserGeoID(int geoId);
    [DllImport("kernel32.dll")]
    static extern int GetGeoInfo(int geoid, int GeoType, StringBuilder lpGeoData, int cchData, int langid);
    [DllImport("kernel32.dll")]
    static extern int GetUserDefaultLCID();
    
    static void Main(string[] args)
    {
      int geoId = GetUserGeoID(GEOCLASS_NATION);
      string friendlyName = GetGeoFriendlyName(geoId);
    }
    
    
    private string GetGeoFriendlyName(int geoId)
    {
      int lcid = GetUserDefaultLCID();
      StringBuilder bldr = new StringBuilder(50);
      GetGeoInfo(geoId, GEO_FRIENDLYNAME, bldr, bldr.Capacity, lcid);
    
      return bldr.ToString();
    }
    
    

    Brad

    http://www.renchdev.com

    • Proposed as answer by Doigen Sunday, October 2, 2011 4:06 PM
    Friday, July 1, 2011 10:50 AM