How to Get Current Regional Settings in C#

How to get current regional settings in C#?

As @Christian proposed ClearCachedData is the method to use. But according to MSDN:

The ClearCachedData method does not
refresh the information in the
Thread.CurrentCulture property for
existing threads

So you will need to first call the function and then start a new thread. In this new thread you can use the CurrentCulture to obtain the fresh values of the culture.

class Program
{
private class State
{
public CultureInfo Result { get; set; }
}

static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture.ClearCachedData();
var thread = new Thread(
s => ((State)s).Result = Thread.CurrentThread.CurrentCulture);
var state = new State();
thread.Start(state);
thread.Join();
var culture = state.Result;
// Do something with the culture
}

}

Note, that if you also need to reset CurrentUICulture, you should do it separately

Thread.CurrentThread.CurrentUICulture.ClearCachedData()

In C#, how to get the Country or region selected under Region & language in Windows 10?

I found the answer to my question in this thread.

I am using the below code, as proposed by @SanjaySingh in that thread and only slightly modified.

If I call GetMachineCurrentLocation with the geoFriendlyname parameter set to 5, I get the three-letter ISO region code I want (for Germany this is "DEU").

The values for geoFriendlyname can be found here.

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>
/// <param name="geoFriendlyname"></param>
public static string GetMachineCurrentLocation(int geoFriendlyname)
{
int geoId = GetUserGeoID(GeoClass.Nation); ;
int lcid = GetUserDefaultLCID();
StringBuilder locationBuffer = new StringBuilder(100);
GetGeoInfo(geoId, geoFriendlyname, locationBuffer, locationBuffer.Capacity, lcid);

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

#endregion
}

How to get Windows region format?

I used this code.

Thread.CurrentThread.CurrentCulture.ClearCachedData();
var thread = new Thread(s => ((State)s).Result = Thread.CurrentThread.CurrentCulture);
var state = new State();
thread.Start(state);
thread.Join();
var culture = state.Result;

And then we use culture.Name.
And in this case it's not necessary to close and reopen the program.

Regional Settings Override in .NET Framework

Here is one method to change the culture for the app at runtime only. In form load all cultures are loaded so you can tinker with them and see results in text box controls for double and date time.

Imports System.Globalization
''' <summary>
''' Requires
''' 1 ComboBox: cboCultures
''' 2 TextBoxes: TextBox1, TextBox2
''' 1 DateTimePicker: DateTimePicker1
''' </summary>
Public Class Form1
' Used to set cboCultures selected item and for restoring the current culture
Private OriginalCultureName As String = ""
Private OriginalCultureIndex As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
OriginalCultureName = Application.CurrentCulture.Name
DateTimePicker1.Format = DateTimePickerFormat.Custom

cboCultures.DisplayMember = "DisplayName"
cboCultures.ValueMember = "Code"
cboCultures.DropDownStyle = ComboBoxStyle.DropDownList

Dim CultureList =
(
From cultureitem In CultureInfo.GetCultures(CultureTypes.SpecificCultures)
Order By cultureitem.EnglishName
Select New With
{
.DisplayName = cultureitem.EnglishName,
.Code = cultureitem.Name
}
).ToList

cboCultures.DataSource = CultureList

' get current culture so we can select it in cboCultures
Dim item =
(
From T In CultureList _
.Select(
Function(cultureitem, indexer)
Return New With {.Index = indexer, .Name = cultureitem.Code}
End Function)
Where T.Name = OriginalCultureName
).First

OriginalCultureIndex = item.Index
cboCultures.SelectedIndex = OriginalCultureIndex
End Sub

Private Sub cmdChange_Click(sender As Object, e As EventArgs) Handles cmdChange.Click
Application.CurrentCulture = New CultureInfo(cboCultures.SelectedValue.ToString)
Demo()
End Sub
''' <summary>
''' Show results for date and double
''' </summary>
Private Sub Demo()
DateTimePicker1.CustomFormat = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern
TextBox1.Text = DateTimePicker1.Value.ToString
Dim value As Double = 123.456789
TextBox2.Text = value.ToString
End Sub
Private Sub cmdRestore_Click(sender As Object, e As EventArgs) Handles cmdRestore.Click
Application.CurrentCulture = New CultureInfo(OriginalCultureName)
cboCultures.SelectedIndex = OriginalCultureIndex
cmdChange.PerformClick()
Demo()
End Sub
End Class

Update: I used string formatting as per below and it worked for me

Private Sub Demo()
DateTimePicker1.CustomFormat = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern
TextBox1.Text = DateTimePicker1.Value.ToString
Dim value As Double = 123.456789
TextBox2.Text = value.ToString("c2")
TextBox3.Text = String.Format("{0:c2}", value)
End Sub

enter image description here

enter image description here

Current Regional Settings to C# 2.0

Use an Anonymous Method.

    class Program
{
private class State
{
public CultureInfo Result { get; set; }
}

static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture.ClearCachedData();
Thread thread = new Thread(delegate(object s)
{
((State) s).Result = Thread.CurrentThread.CurrentCulture;
});
State state = new State();
thread.Start(state);
thread.Join();
CultureInfo culture = state.Result;
// Do something with the culture
}
}

How take decimal symbol (string/char) in current region?

char regionSymbol = (1.1).ToString()[1];

How to get the current region DateFormat like dd/mm/yyyy

When you get a CultureInfo object, it has a DateTimeFormat property on it. That has a ShortDatePattern property which shows the pattern it uses to format dates.

From the linked MSDN article:

public class SamplesDTFI  
{
public static void Main()
{
string[] cultures = { "en-US", "ja-JP", "fr-FR" };
DateTime date1 = new DateTime(2011, 5, 1);

Console.WriteLine(" {0,7} {1,19} {2,10}\n", "CULTURE", "PROPERTY VALUE", "DATE");

foreach (var culture in cultures) {
DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture(culture).DateTimeFormat;
Console.WriteLine(" {0,7} {1,19} {2,10}", culture,
dtfi.ShortDatePattern,
date1.ToString("d", dtfi));
}
}
}
// The example displays the following output:
// CULTURE PROPERTY VALUE DATE
//
// en-US M/d/yyyy 5/1/2011
// ja-JP yyyy/MM/dd 2011/05/01
// fr-FR dd/MM/yyyy 01/05/2011


Related Topics



Leave a reply



Submit