1

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.

Whatever
  • 77
  • 1
  • 12

2 Answers2

1

Okay never mind I found the solution:

For WPF it is:

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

See: Detect system language change in WPF

For Windows Forms you can use:

string test = InputLanguage.CurrentInputLanguage.Culture.Name;

See: Detect current keyboard language/layout name in multi-language computer

Whatever
  • 77
  • 1
  • 12
  • 1
    I note that `InputLanguage.CurrentInputLanguage` is in `System.Windows.Forms` so it isn't suitable/available in non-WinForms projects like WPF, ASP.NET, Console, Windows Services, etc. In .NET Core / .NET 5+ it's only available in .NET projects targeting the "Windows Desktop" platform. – Dai Feb 02 '22 at 09:15
  • Thanks @Dai I did not notice that, but good point as I want to use it in a WPF context. Luckily there is a System.Windows.Input equivalent: `string currentKeyboardLayout = System.Windows.Input.InputLanguageManager.Current.CurrentInputLanguage.Name;` – Whatever Feb 02 '22 at 09:26
0

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;
}

Source

Elikill58
  • 4,050
  • 24
  • 23
  • 45