我试图获取所有的英文
这是我机器上的结果,下面是文本内容:
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' が見つかりませんでした。
“日本的反斜杠”在日本机器上显示与我的机器上显示不同,但复制到我的机器上时会显示为“\”。
综合各种答案,我已经实现了以下解决方案,现在它看起来像这样:
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),但结果都是一样的。
- 是否有一个完整的解决方案适用于所有的异常情况?
- 我有漏掉什么吗?
- 我应该做些什么?
- 也许还有其他的方法可以在外国机器上运行程序,并设置一些环境变量来获取英语输出吗?