Get current location (as indicated by region and language) in C #

How can I get the current location of the machine indicated in the "Region and OS language" section? I already tried to get this from the RegionInfo class, but it returns the location specified in the Region and language format drop-down list.

Just to clarify what I mean. If you open “Region and Language” in the control panel of your computer, I want to read “Location” as indicated on the “Location” tab. RegionInfo gives me the value specified in the Format drop-down list on the Formats tab.

+7
source share
4 answers

After long searches, I finally got a response. After two links, help me get the current location of the machine -

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 someone is interested in the 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 } 
+17
source

yes .. but easier:

 CultureInfo info = CultureInfo.CurrentCulture; 
+2
source

Can you try using

RegionInfo.CurrentRegion.DisplayName;

Does this provide you with the desired name of the location you are planning

+1
source

Based on Control Panel> Region> Location, you can get RegionInfo. Try it -

 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)); 
0
source

All Articles