• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#如何使用微软系统类库Microsoft.VisualBasic进行繁体和简体转换

OC/C/C++ 水墨上仙 1341次浏览

C#通过微软系统类库Microsoft.VisualBasic进行繁体和简体转换

添加 Microsoft Visual Basic.NET Runtime 引用。

using Microsoft.VisualBasic

using System;
using System.IO;
using System.Text;
using Microsoft.VisualBasic;

namespace Rainsoft.Text
{
  /// <summary>
  /// 字符串编码转换类
  /// </summary>
  public class StringEncoding
  {
    private StringEncoding()
    {
    }

    /// <summary>
    /// 将字符串转换为简体中文
    /// </summary>
    public static string ToSimplifiedChinese(string s)
    {
      return Microsoft.VisualBasic.Strings.StrConv(s, VbStrConv.SimplifiedChinese, 0);
    }

    /// <summary>
    /// 将文件转换为简体中文
    /// </summary>
    /// <param name="filename">源文件名</param>
    /// <param name="encoding">源文件字符编码</param>
    /// <param name="outFilename">目标文件名</param>
    /// <param name="outEncoding">目标文件字符编码</param>
    /// <example>
    /// <code>
    /// ToSimplifiedChinese("big5.txt", Encoding.GetEncoding("big5"), "gb.txt", Encoding.GetEncoding("gb2312"));
    /// ToSimplifiedChinese("big5.txt", Encoding.GetEncoding("big5"), "gb.txt", Encoding.UTF8);
    /// </code>
    /// </example>
    public static void ToSimplifiedChinese(string filename, Encoding encoding, string outFilename, Encoding outEncoding)
    {
      StreamReader r = new StreamReader(filename, encoding);
      StreamWriter w = new StreamWriter(outFilename, false, outEncoding);
      try
      {
        w.Write(Strings.StrConv(r.ReadToEnd(), VbStrConv.SimplifiedChinese, 0));
        w.Flush();
      }
      finally
      {
        w.Close();
        r.Close();
      }
    }

    /// <summary>
    /// 将字符串转换为繁体中文
    /// </summary>
    public static string ToTraditionalChinese(string s)
    {
      return Microsoft.VisualBasic.Strings.StrConv(s, VbStrConv.TraditionalChinese, 0);
    }

    /// <summary>
    /// 将文件转换为繁体中文
    /// </summary>
    /// <param name="filename">源文件名</param>
    /// <param name="encoding">源文件字符编码</param>
    /// <param name="outFilename">目标文件名</param>
    /// <param name="outEncoding">目标文件字符编码</param>
    /// <example>
    /// <code>
    /// ToTraditionalChinese("gb.txt", Encoding.GetEncoding("gb2312"), "gb.txt", Encoding.GetEncoding("big5"));
    /// ToTraditionalChinese("gb.txt", Encoding.GetEncoding("gb2312"), "gb.txt", Encoding.UTF8);
    /// </code>
    /// </example>
    public static void ToTraditionalChinese(string filename, Encoding encoding, string outFilename, Encoding outEncoding)
    {
      StreamReader r = new StreamReader(filename, encoding);
      StreamWriter w = new StreamWriter(outFilename, false, outEncoding);
      try
      {
        w.Write(Strings.StrConv(r.ReadToEnd(), VbStrConv.TraditionalChinese, 0));
        w.Flush();
      }
      finally
      {
        w.Close();
        r.Close();
      }
    }
  }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#如何使用微软系统类库Microsoft.VisualBasic进行繁体和简体转换
喜欢 (0)
加载中……