解决因为编码方式导致的乱码

using System.Text;
using System.IO;

namespace EncodingReader
{
   
    class Program
    {
        static void Main(string[] args)
        {
            try {
                if (args.Length == 0)
                {
                    Console.Write("请输入完整的命令格式,例如 EncodingReader.exe test.txt");
                }
                else {
                    String strFullName = String.Empty;
                    for (int i = 0; i < args.Length; i++) {
                        strFullName += args[i];
                    }
                    if (!File.Exists(strFullName)) {
                        Console.WriteLine("文件"+strFullName +"不存在");
                    }
                    else {
                        FileInfo fi = new FileInfo(strFullName);
                        string strPath = strFullName.Substring(0, strFullName.Length - fi.Name.Length);
                        string strFileName = fi.Name;
                        string[] enumEncoding = new string[] {"big5","x-mac-chinesetrad","x-Chinese-CNS",
                            "x-cp20001","x-Chinese-Eten","x-cp20003","x-cp20004","x-cp20005",
                            "utf-8", "utf-7", "utf-16", "UnicodeFFFE", "utf-32", "utf-32BE",
                            "gb2312","GB18030", "hz-gb-2312", "EUC-CN", "x-cp50227", "x-mac-chinesesimp" };
                        for(int i=0;i<enumEncoding.Length ;i++){
                            StreamReader sr = new StreamReader(strFullName,Encoding.GetEncoding(enumEncoding[i]));
                            StreamWriter sw = new StreamWriter(strPath + enumEncoding[i] + "." + strFileName,
                                false, Encoding.Unicode);
                            sw.Write(sr.ReadToEnd());
                            sw.Flush();
                            sw.Close();
                            sr.Close();
                        }
                    }
                }
            }
            catch (Exception excep)
            {
                Console.WriteLine(excep.ToString());
            }
            finally {
                Console.WriteLine("请按任一键退出");
                Console.Read();
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值