gpt4 book ai didi

c# - 如何获取英文的Win32Exception?

转载 作者:太空狗 更新时间:2023-10-29 23:13:34 24 4
gpt4 key购买 nike

我正在尝试以英文获取所有Exception 消息,无论我的程序运行在何种语言的机器上。

我使用以下帖子的答案设法获得了几乎所有的英文异常消息: Exception messages in English?和我发现的其他一些解决方案(比如使用反射来更改默认的 CultureInfo)。我有关于 SocketException 的特定问题,无论我在做什么,我都会以默认机器的语言获取它。

我创建了一个测试程序来显示问题:该测试程序将以默认语言打印异常:

using System;
using System.Text;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Reflection;
using System.Globalization;

namespace TestApp
{
class Program
{
static void Main(string[] args)
{
try
{
//I'm not listening on the following port:
TcpClient s = new TcpClient("localhost", 2121);
}
catch (Exception ex)
{
Console.WriteLine("Socket exception: " + ex.Message);
}
try
{
//the following file doesn't exists:
File.ReadAllText("filenotexist.txt");
}
catch (Exception ex)
{
Console.WriteLine("File exception: " + ex.Message);
}
}
}
}

此结果在我的机器上显示以下文本:

H:\Shared>Test-def.exe
Socket exception: No connection could be made because the target machine actively refused it 127.0.0.1:2121
File exception: Could not find file 'H:\Shared\filenotexist.txt'.

在日文机器上它用日文写所有异常(我不懂):

Z:\>Test-def.exe
Socket exception: 対象のコンピューターによって拒否されたため、接続できませんでした。 127.0.0.1:2121
File exception: ファイル 'Z:\filenotexist.txt' が見つかりませんでした。

(日文的'\'在日 native 器上看起来不一样,但是复制到我的机器上显示为'\')

结合我找到的答案,我实现了以下解决方案,现在看起来像这样:

namespace TestApp
{
class Program
{
//will change CultureInfo to English, this should change all threads CultureInfo to English.
public static void SetEnglishCulture()
{
CultureInfo ci = new CultureInfo("en-US");
//change CultureInfo for current thread:
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;

//change CultureInfo for new threads:
Type t = typeof(CultureInfo);
try
{
t.InvokeMember("s_userDefaultCulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
t.InvokeMember("s_userDefaultUICulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
}
catch { }
try
{
t.InvokeMember("m_userDefaultCulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
t.InvokeMember("m_userDefaultUICulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
}
catch { }
}
static void Main(string[] args)
{
//first thing: set CultureInfo to English:
SetEnglishCulture();
try
{
//I'm not listening on the following port:
TcpClient s = new TcpClient("localhost", 2121);
}
catch (Exception ex)
{
Console.WriteLine("Socket exception: " + ex.Message);
}
try
{
//the following file doesn't exists:
File.ReadAllText("filenotexist.txt");
}
catch (Exception ex)
{
Console.WriteLine("File exception: " + ex.Message);
}
}
}
}

现在在日文机器上它用英文写文件异常,但 Net.socket 异常仍然是日文:

Z:\>Test-en.exe
Socket exception: 対象のコンピューターによって拒否されたため、接続できませんでした。 127.0.0.1:2121
File exception: Could not find file 'Z:\filenotexist.txt'.

我还测试了其他一些异常,现在有些异常显示为英文,但不是全部,套接字异常是持久化的。如您所见,文件异常已被翻译成英文,但套接字异常仍然是日文。

我已经在几乎任何 .NET 框架(从 2.1 到 4.5)中测试过它,但仍然一样。

  • 是否有针对所有异常的完整解决方案?
  • 我错过了什么吗?
  • 我还需要做其他事情吗?
  • 也许有其他方法可以在外国机器上运行程序,并设置一些环境变量,以获得英文输出?

最佳答案

我有一个解决方案,所以我会上传到这里以备不时之需。如果有人有更好的解决方案,我会很高兴知道,所以请发表评论。

Win32Exception 的情况下,我们可以使用 FormatMessage 并将错误代码翻译成英语和默认语言,并将默认值替换为英语。如果我在没有替换的情况下使用英文,我会丢失参数。因此,如果替换失败,我将返回带有附加英文描述的异常。

这是我的完整解决方案:

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Globalization;
using System.Reflection;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace TestCulture
{
class Program
{
static void SetEnglishCulture()
{
CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
Type type = typeof(CultureInfo);
try
{
type.InvokeMember("s_userDefaultCulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
type.InvokeMember("s_userDefaultUICulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
} catch { }
try
{
type.InvokeMember("m_userDefaultCulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
type.InvokeMember("m_userDefaultUICulture", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Static, null, ci, new object[] { ci });
} catch { }
}
[DllImport("kernel32.dll")]
static extern uint FormatMessage(uint dwFlags, IntPtr lpSource, uint dwMessageId, uint dwLanguageId, StringBuilder lpBuffer, uint nSize, IntPtr Arguments);
public static string Win32ExceptionInEnglish(Win32Exception ex)
{
const int nCapacity = 820; // max error length
const uint FORMAT_MSG_FROM_SYS = 0x01000;
const uint engLangID = (0x01<<10) | 0x09;
const uint defLangID = 0x0;
StringBuilder engSb = new StringBuilder(nCapacity);
StringBuilder defSb = new StringBuilder(nCapacity);
FormatMessage(FORMAT_MSG_FROM_SYS,IntPtr.Zero, (uint)ex.ErrorCode, defLangID, defSb, nCapacity, IntPtr.Zero);
FormatMessage(FORMAT_MSG_FROM_SYS,IntPtr.Zero, (uint)ex.ErrorCode, engLangID, engSb, nCapacity, IntPtr.Zero);
string sDefMsg = defSb.ToString().TrimEnd(' ','.','\r','\n');
string sEngMsg = engSb.ToString().TrimEnd(' ','.','\r','\n');
if(sDefMsg == sEngMsg) //message already in English (or no english on machine?)
{
//nothing left to do:
return ex.Message;
}
else
{
string msg = ex.Message.Replace(sDefMsg,sEngMsg);
if (msg == ex.Message)
{
//replace didn't worked, can be message with arguments in the middle.
//I such as case print both: original and translated. to not lose the arguments.
return ex.Message + " (In English: " + sEngMsg + ")";
}
else
{
//successfuly replaced!
return msg;
}
}
}

public static void Main(string[] args)
{
SetEnglishCulture();
try {
// generate any exception ...
const int notListenningPort = 2121;
new TcpClient("localhost", notListenningPort);
}
catch(Win32Exception ex)//first try to cach win32 Exceptions
{
Console.WriteLine("W32 Exception: " + Win32ExceptionInEnglish(ex));
}
catch(Exception ex)//this fit to the rest .NET exceptions which affected by CultureInfo
{
Console.WriteLine("Exception: " +ex.Message);
}
}
}
}

关于c# - 如何获取英文的Win32Exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34423129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com