简体   繁体   English

如何从 windows 中的当前键盘布局中获取文化信息?

[英]How to get culture information from current keyboard layout in windows?

I want to copy decimal values from my program to excel and vice versa.我想将十进制值从我的程序复制到 excel ,反之亦然。 My program formats the double values to a string with "."我的程序将双精度值格式化为带有“。”的字符串。 as decimal separator, but if I paste the values into excel they will be formatted as dates because my excel program somehow works with a different culture, even if my OS is English and Excel is installed in English, but I use a German keyboard layout.作为小数点分隔符,但如果我将值粘贴到 excel 中,它们将被格式化为日期,因为我的 excel 程序以某种方式适用于不同的文化,即使我的操作系统是英语并且 ZC1D81AF5835844B4E9D936910DED8 安装了英语键盘,但我使用的是德语键盘布局。 So i guess excel determines its culture formatting from that.所以我猜 excel 从中确定它的文化格式。

I know that I can prevent the parsing errors by parameterizing the culture in to the.ToString() and Parse method like that:我知道我可以通过将区域性参数化为 .ToString() 和 Parse 方法来防止解析错误,如下所示:

value.ToString(CultureInfo.CreateSpecificCulture("de-DE"));
double.TryParse(string, NumberStyles.Float, CultureInfo.CreateSpecificCulture("de-DE"), out y);

The problem is that I do not know the culture of my clients, they work all over the world and my program is on English.问题是我不了解客户的文化,他们在世界各地工作,而我的课程是英语。 But I can't just use the English format as it causes trouble when pasting into excel.但我不能只使用英文格式,因为它在粘贴到 excel 时会引起麻烦。

I tried to get the current culture through:我试图通过以下方式了解当前的文化:

CultureInfo currentCulture = CultureInfo.CurrentUICulture;
CultureInfo currentCulture = CultureInfo.CurrentCulture;

The first one returns "en-US" the second one returns "" invariant culture.第一个返回“en-US”,第二个返回“”不变文化。 So that does not help me at all.所以这对我一点帮助都没有。

Is there a way to read out the culture information from the current active keyboard layout from windows?有没有办法从 windows 的当前活动键盘布局中读出文化信息? Or is it possible to retrieve the information from excel directly?或者是否可以直接从 excel 中检索信息? Or does someone have a better solution for my problem?或者有人对我的问题有更好的解决方案吗? I do not think I am the first one encountering this, but I could not find anything helpful on google.我不认为我是第一个遇到这种情况的人,但我在谷歌上找不到任何有用的东西。

Okay never mind I found the solution:好吧,没关系,我找到了解决方案:

For WPF it is:对于 WPF 它是:

string currentKeyboardLayout = System.Windows.Input.InputLanguageManager.Current.CurrentInputLanguage.Name;
CultureInfo culture = CultureInfo.CreateSpecificCulture(currentKeyboardLayout);

See: Detect system language change in WPF请参阅: 检测 WPF 中的系统语言更改

For Windows Forms you can use:对于 Windows Forms,您可以使用:

string test = InputLanguage.CurrentInputLanguage.Culture.Name;

See: Detect current keyboard language/layout name in multi-language computer请参阅: 检测多语言计算机中的当前键盘语言/布局名称

public void MyCurrentInputLanguage() {
// Gets the current input language  and prints it in a text box.
InputLanguage myCurrentLanguage = InputLanguage.CurrentInputLanguage;
textBox1.Text = "Current input language is: " +
myCurrentLanguage.Culture.EnglishName;
}

can be a method used to get the input language and show how to use it with 
your textbox display.

the code is from 
https://docs.microsoft.com/en- 
us/dotnet/api/system.windows.forms.inputlanguage.currentinputlanguage? 
view=windowsdesktop-6.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM