Setting Default CurrentCulture in all Versions of .NET
If you are using any version of .NET prior to 4.5, you will not have access to the handy property that is DefaultThreadCurrentCulture (or if you are using .NET 4.5 just stop reading here and use that property!). However, that does not mean you can’t set the default CultureInfo to be used across the current thread and all threads spawned afterwards in the one place.
The CultureInfo class has two private static members named m_userDefaultCulture and m_userDefaultUICulture in versions prior to .NET 4.0; in 4.0 they are named s_userDefaultCulture and s_userDefaultUICulture.
These two members are used when calling Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture, and with some simple reflection we can set these members, and all calls from there on to these two properties will return our newly set default.
Copy and paste the method below into your project wherever you see fit, ensuring that you are using both System.Globalization and System.Reflection at the top of the file:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public void SetDefaultCulture(CultureInfo culture)
{
Type type = typeof(CultureInfo);
try
{
type.InvokeMember("s_userDefaultCulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
type.InvokeMember("s_userDefaultUICulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
}
catch { }
try
{
type.InvokeMember("m_userDefaultCulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
type.InvokeMember("m_userDefaultUICulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
}
catch { }
} |
Just to give a slight overview of what is happening in that method re. the try catch blocks – as mentioned previously, the names of these static members differs between .NET 4.0 and versions prior, so this will attempt to set both and fall back to whichever one it needs.
Once you’ve added the method, simply call it and pass through the CultureInfo you wish to be the default and you’re done!
Below is a full example of how it can be used, or alternatively you can Download This Sample Project.
Enjoy!
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
namespace DefaultCultureInfoExample
{
class Program
{
static void SetDefaultCulture(CultureInfo culture)
{
Type type = typeof(CultureInfo);
try
{
type.InvokeMember("s_userDefaultCulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
type.InvokeMember("s_userDefaultUICulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
}
catch { }
try
{
type.InvokeMember("m_userDefaultCulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
type.InvokeMember("m_userDefaultUICulture",
BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static,
null,
culture,
new object[] { culture });
}
catch { }
}
static void Main(string[] args)
{
Console.WriteLine("Culture name before update: {0}",
Thread.CurrentThread.CurrentCulture.Name);
SetDefaultCulture(new CultureInfo("fr-fr"));
Console.WriteLine("Culture name after update: {0}",
Thread.CurrentThread.CurrentCulture.Name);
Console.ReadLine();
}
}
} |
Got anything to say? Go ahead and leave a comment!
Archives
Recent Posts
- Adding Both Undo and Redo Functionality to the .NET TextBox
- Setting Default CurrentCulture in all Versions of .NET
- Capturing Network Traffic using the WiFi Pineapple, tcpdump and Android
- Disable Shortcuts in WebBrowser Control and keep Copy and Paste
- Cracking PPTP / MS-CHAPv2 with Chapcrack & CloudCracker
Tag Cloud
Recent Comments
- int0x90 on Disable Shortcuts in WebBrowser Control and keep Copy and Paste
- Olivier Jaquemet on Disable Shortcuts in WebBrowser Control and keep Copy and Paste
- ponix on Kid Teaches How to Use Tracert – Fail!
- kat on Final Fantasy XIII: How to Beat Odin
- JD Swagger on Final Fantasy XIII: How to Beat Odin



Posted under: