Get English exception message instead of local language

孤者浪人 提交于 2019-12-22 04:08:51

问题


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.



来源:https://stackoverflow.com/questions/13493717/get-english-exception-message-instead-of-local-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!