从 C# 中检测 os 语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/479300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:00:12  来源:igfitidea点击:

detect os language from c#

c#operating-system

提问by crauscher

Is there a way to detect the Language of the OS from within a c# class?

有没有办法从 ac# 类中检测操作系统的语言?

采纳答案by Oliver Friedrich

With the System.Globalization.CultureInfoclass you can determine what you want.

通过System.Globalization.CultureInfo课程,您可以确定自己想要什么。

With CultureInfo.CurrentCultureyou get the system set culture, with CultureInfo.CurrentUICultureyou get the user set culture.

随着CultureInfo.CurrentCulture您获得系统集文化,CultureInfo.CurrentUICulture您将获得用户集文化。

回答by Bevan

Do you mean whether the machine is configured (e.g.) with English, French or Japanese?

您的意思是机器是否配置(例如)英语、法语或日语?

Have a look at the CultureInfo class - particularly CurrentCulture, which is initialised from the OS current regional settings.

查看 CultureInfo 类 - 特别是 CurrentCulture,它是从操作系统当前区域设置初始化的。

回答by Bevan

System.Threading.Thread.CurrentThread.CurrentCulture

System.Threading.Thread.CurrentThread.CurrentCulture

回答by Mihai Drebot

Unfortunately, the previous answers are not 100% correct.

不幸的是,之前的答案并非 100% 正确。

The CurrentCultureis the culture info of the running thread, and it is used for operations that need to know the current culture, but not do display anything. CurrentUICultureis used to format the display, such as correct display of the DateTime. Because you might change the current thread Cultureor UICulture, if you want to know what the OS CultureInfoactually is, use CultureInfo.InstalledUICulture.

CurrentCulture是正在运行的线程的文化信息,它是用于需要知道当前的文化,但不做任何显示操作。CurrentUICulture用于格式化显示,例如正确显示DateTime. 因为您可能会更改当前线程,Culture或者UICulture,如果您想知道操作系统的CultureInfo实际情况,请使用CultureInfo.InstalledUICulture.

Also, there is another question about this subject (more recent than this one) with a detailed answer:

此外,还有关于这个主题的另一个问题(比这个问题更近),有详细的答案:

Get operating system language in c#.

在 c# 中获取操作系统语言

回答by Lost

"System.Globalization.CultureInfo.CurrentUICulture.DisplayName". This is exactly what you want.

"System.Globalization.CultureInfo.CurrentUICulture.DisplayName". 这正是您想要的。

回答by Mark Green

Means that if the system locale on Region and Language, you can use Win32 API function GetSystemDefaultLCID. The signiture is as follow:

意味着如果系统区域设置在Region 和Language 上,则可以使用Win32 API 函数GetSystemDefaultLCID。签名如下:

[DllImport("kernel32.dll")]
static extern uint GetSystemDefaultLCID();

GetSystemDefaultLCID function returns the LCID. It can map language string from the folowing table. Locale IDs Assigned by Microsoft

GetSystemDefaultLCID 函数返回 LCID。它可以映射下表中的语言字符串。 Microsoft 分配的区域设置 ID

回答by Mike Parkhill

All of the answers above seem to only get you the OS installed culture (at best). I ran into an issue where I needed the actual display language being used in Windows. i.e. the user installed the default en-US Windows install, but then added a language pack for German (de-DE) and set that as their display language.

上面的所有答案似乎只能让您了解已安装操作系统的文化(充其量)。我遇到了一个问题,我需要在 Windows 中使用实际的显示语言。即用户安装了默认的 en-US Windows 安装,但随后添加了德语 (de-DE) 语言包并将其设置为显示语言。

To get that, I used System.Windows.Input.InputLanguageManager.Current.CurrentInputLanguage

为此,我使用了System.Windows.Input.InputLanguageManager.Current.CurrentInputLanguage