'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. 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. So i guess excel determines its culture formatting from that.
I know that I can prevent the parsing errors by parameterizing the culture in to the .ToString() and Parse method like that:
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.
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. 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? Or is it possible to retrieve the information from excel directly? 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.
Solution 1:[1]
This method can be used to get the input language and show how to use it with your textbox display.
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elikill58 |
