Get English exception message instead of local lan

2020-08-21 06:54发布

问题:

On Windows 8 using Visual Studio 2012 RC on a german system, I get all my Exceptions localized to german, which effectively means I can't google anything useful for them. To solve this, I already used the following to change my IDE to english language:

Tools --> Options --> Internetional Settings --> Language --> English

Nevertheless, I get my exceptions in the localized german language. I tried changing the ThreadUI Culture in code using this code:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");

Sadly, in WinRT the Thread namespace is gone in WinRT. Therefore I tried:

System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-us");

I get the german exception message still. Does anyone know how to get the un-localized version of the exception messages?

回答1:

Your other option is to retrieve and display the Exception.HResult value, which can be searched upon and turned into a useful error message in English.

Another possibility, if these exceptions have Win32 codes, albeit a hack:

[DllImport("kernel32.dll",
           EntryPoint = "FormatMessageW",
           SetLastError = true,
           CharSet = CharSet.Auto)]
private static extern int FormatMessage(
    int dwFlags,
    IntPtr lpSource,
    int dwMessageId,
    int dwLanguageId,
    StringBuilder lpBuffer,
    int nSize,
    IntPtr[] Arguments);

// used like:
var builder = new StringBuilder(2048);
var res = FormatMessage(
    0x1000|0x0200/*System Message, Ignore Inserts*/,
    IntPtr.Zero,
    exception.HResult,
    new CultureInfo("en-US").LCID,
    builder,
    builder.Capacity,
    null);
 Console.WriteLine("{0}", builder.ToString());
 // throw new StackOverflowException()
 // "Recursion too deep; the stack overflowed."


回答2:

Exceptions have localized messages by design, it's desired behavior. You should change your local computer settings.