Changing the 'Region and Language' OS setting programmatically

梦想与她 提交于 2019-11-28 02:00:31

The only solution I managed to implement was to modify the registry. In Windows 7, when the language is changed, a new entry is added to the Registry in the subkey: HKEY_CURRENT_USER\Control Panel\Desktop. This key will contain the entry PreferredUILanguagesPending of type REG_MULTI_SZ and its value will determine the UI language. For the change to be applied the current user needs to log off and log in again. This can be done using the following code:

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
string[] lang = {"en-ZA"};
key.SetValue("PreferredUILanguagesPending", lang, RegistryValueKind.MultiString);

The language pack needs to be installed before it can be set. For a list of language packs check here or here. When more than 1 language pack is installed option to change the UI language will appear in Control Panel > Region and Language > Keyboards and Languages > Display language.

Sounds to me as if changing the Culture/UICulture of your application should be sufficient

e.g.

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");

I've found a good replacement for the DateTimePicker: http://www.visualhint.com/fieldpackeditor

You will have the same problems with all the system controls and system dialogs like OpenFileDialog, PrintDialog, etc., they are not localizable in .NET.

But thinking about it, why would you want to change the culture for your application? The user can change his region and language settings by himself using the control panel, why should your application overwrite those settings?

Siva Gopal

I think, you are thinking in opposite to what you need. Often application(s) may need to help/honor/satisfy user OS settings than changing them for app's convenience. Have a look at the post with problem similar to your's : How can I change a Windows user's regional settings/date format?

I found a solution here: https://support.microsoft.com/de-de/help/2764405/how-to-automate-regional-and-language-settings-in-windows-vista-window

You need to create an XML file with the following content:

<gs:GlobalizationServices xmlns:gs="urn:longhornGlobalizationUnattend">
    <gs:UserList>
        <gs:User UserID="Current"/>
    </gs:UserList>
    <gs:UserLocale>
        <gs:Locale Name="en-US" SetAsCurrent="true"/>
    </gs:UserLocale>
</gs:GlobalizationServices>

Then run this command:

control intl.cpl,, /f:"<path to XML file>"

This would set the format to English (United States) for the currently logged in user.

Although the article is about Windows Vista, this also works in Windows 7.

TomTom

This is a language localisation problem where Controls like DateTimePicker can only use the Windows >Region and Language settings

NUTS. Seriously. Think about what you do here; I start your program and you reconfigure my whole computer to make a small UI element work? Preferably without asking or telling me?

There are laws against that - this can be seen as unauthorized manipulation of a computer.

Use a proper control and properly program; but do not commit sabotage on that level.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!