我正在努力得到所有Exception
消息都是英文的,无论我的程序运行在什么语言上。
我已经使用以下帖子中的答案设法获得了几乎所有英文异常消息:异常消息是英文的吗? https://stackoverflow.com/questions/209133/c-sharp-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' が見つかりませんでした。
(日语“\”在日语机器中看起来不同,但是当复制到我的机器时它显示为“\”)
因此,通过结合我找到的答案,我实现了以下解决方案,所以现在看起来像这样:
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)中对其进行了测试,结果仍然相同。
- 是否有针对所有异常的完整解决方案?
- 我错过了什么吗?
- 我还应该做点别的吗?
- 也许还有其他方法可以在外国机器上运行程序,并设置一些环境变量,以获得英文输出?