Mono是运行在Linux Mac OS 等系统上的.NET环境,通过Mono可以使.NET达到跨平台的目的.但是乱码问题一直是跨平台最头疼的问题.下面给出一种解决方案
首先说一下我的环境:asus+fedora 3 + Mono 1.1。
Locale设置全部为zh_CN.gb2312。
在这篇文章中,我先来介绍一下如何处理以前困扰大家的中文问题。下一篇文章我将介绍程序的国际化。
使用monodevlop建立一个Console工程,名称为Test。
编辑Main.cs文件:
using System;
class MainClass { public static void Main(string[] args) {
Console.WriteLine("兄弟的email地址为:smallnest@gmail.com");
} } |
在monodevelop中运行,在输出窗口会显示正确的结果。
打开终端,进入到./bin/Debug目录下,运行mono Test.exe,显示乱码。这和我的环境设置有关,将终端的编码设置为utf8,再运行mono Test.exe,结果显示正常。
以上编译是通过monodevelop编译的,下面手工编译一下,试试效果。
进入Main.cs所在的文件夹:
mcs Main.cs |
执行上述命令后将生成Main.exe文件,运行这个文件。
mono Main.exe |
输出结果为乱码(无论终端编码是gb2312还是utf8)。
通过设置代码页进行编译:
mcs –codepage:utf8 Main.cs |
执行上述命令后将生成Main.exe文件,运行这个文件。
mono Main.exe |
输出结果为正常(终端编码是utf8)。
小技巧:
每次运行mono编译得程序都必须使用mono *.exe,比较麻烦,我们页可以在Linux下创建文件关联。
执行下面得shell就不必每次都运行mono文件了。
if [ ! -e /proc/sys/fs/binfmt_misc/register ]; then /sbin/modprobe binfmt_misc mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
fi if [ -e /proc/sys/fs/binfmt_misc/register ]; then echo '':CLR:M::MZ::/usr/bin/mono:'' > /proc/sys/fs/binfmt_misc/register
else
echo "No binfmt_misc support" exit 1
fi
|
添加评论