Changing the 'Region and Language' OS sett

2020-02-01 08:21发布

I'd like to be able to change the Region and Language settings of the operating system (Windows 7) from a C# program. I'm not against executing command-line commands but I've only gotten as far as discovering how to launch the Region and Language dialog: control /name Microsoft.RegionAndLanguage

This is a language localisation problem where Controls like DateTimePicker can only use the Windows Region and Language settings (see here for details); however updating the operating system to conform to the application's language settings extends beyond this and is ultimately the desired goal.

Suggestions and/or workarounds would be appreciated.

6条回答
虎瘦雄心在
2楼-- · 2020-02-01 08:38

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.

查看更多
▲ chillily
3楼-- · 2020-02-01 08:43

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-02-01 08:46

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");
查看更多
唯我独甜
5楼-- · 2020-02-01 08:46

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.

查看更多
霸刀☆藐视天下
6楼-- · 2020-02-01 08:56

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?

查看更多
一夜七次
7楼-- · 2020-02-01 09:04

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?

查看更多
登录 后发表回答