获取英语异常消息,而不是本地语言



在德国系统上使用Visual Studio 2012 RC的Windows 8上,我将所有异常都本地化为德语,这实际上意味着我无法谷歌任何对他们有用的东西。为了解决这个问题,我已经使用了以下内容将我的 IDE 更改为英语:

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

尽管如此,我还是得到了本地化德语的例外。我尝试使用以下代码更改代码中的 ThreadUI 区域性:

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

可悲的是,在WinRT中,Thread命名空间在WinRT中消失了。因此,我尝试了:

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

我仍然收到德语异常消息。有谁知道如何获取异常消息的未本地化版本?

您的另一个选择是检索并显示Exception.HResult值,可以搜索该值并将其转换为有用的英语错误消息。

另一种可能性,如果这些异常具有Win32代码,尽管是黑客:

[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."

异常在设计上具有本地化消息,这是所需的行为。您应该更改本地计算机设置。

相关内容

最新更新