Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Location (as specified in Region and Language) in C#

How can I get machine's current location as specified in Region and Language of the OS? I have already tried getting this from the RegionInfo class but it returns Location as specified in Format drop down of Region and Language.

Just to clarify what I mean, If you open Region And Language from Control Panel of your machine, I want to read Location as specified in Location tab. RegionInfo gives me value as specified in Format dropdown of Formats tab.

like image 559
Sanjay Singh Avatar asked Jan 16 '12 11:01

Sanjay Singh


3 Answers

After lot of googling, finally i got the answer. Following two links help me get the current machine location-

http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/6dfaa142-c588-4cb0-b044-fa1e8138b299

http://www.siao2.com/2007/02/21/1733999.aspx

I made the following utility class if anybody is interested in final code-

public static class RegionAndLanguageHelper
{
    #region Constants

    private const int GEO_FRIENDLYNAME = 8;

    #endregion

    #region Private Enums

    private enum GeoClass : int
    {
        Nation = 16,
        Region = 14,
    };

    #endregion

    #region Win32 Declarations

    [DllImport("kernel32.dll", ExactSpelling = true, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    private static extern int GetUserGeoID(GeoClass geoClass);

    [DllImport("kernel32.dll")]
    private static extern int GetUserDefaultLCID();

    [DllImport("kernel32.dll")]
    private static extern int GetGeoInfo(int geoid, int geoType, StringBuilder lpGeoData, int cchData, int langid);

    #endregion

    #region Public Methods

    /// <summary>
    /// Returns machine current location as specified in Region and Language settings.
    /// </summary>
    public static string GetMachineCurrentLocation()
    {
        int geoId = GetUserGeoID(GeoClass.Nation); ;
        int lcid = GetUserDefaultLCID();
        StringBuilder locationBuffer = new StringBuilder(100);
        GetGeoInfo(geoId, GEO_FRIENDLYNAME, locationBuffer, locationBuffer.Capacity, lcid);

        return locationBuffer.ToString().Trim();
    }

    #endregion
}
like image 156
Sanjay Singh Avatar answered Oct 16 '22 04:10

Sanjay Singh


Can you try using

RegionInfo.CurrentRegion.DisplayName;

Does this give you the required Location name as you intend

like image 1
V4Vendetta Avatar answered Oct 16 '22 04:10

V4Vendetta


Base on "Control Panel > Region > Home Location", you can get RegionInfo. Try this -

var regKeyGeoId = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Control Panel\International\Geo");
var geoID = (string)regKeyGeoId.GetValue("Nation");
var allRegions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.ToString()));
var regionInfo = allRegions.FirstOrDefault(r => r.GeoId == Int32.Parse(geoID));
like image 1
Zan Avatar answered Oct 16 '22 02:10

Zan