首先需要获取ChnCharInfo.dll (汉子转拼音就可以了)和ChineseConverter.dll (简繁转化吧)
ChnCharInfo.dll官方下载操作如下:
先下载微软Microsoft Visual Studio International Pack 1.0 SR1语言包:
Microsoft Visual Studio International Pack 1.0 SR1
Microsoft Visual Studio International Feature Pack 2.0
下载后分别是“vsintlpack1.zip”、“Vsintlpack2.msi”、双击“Vsintlpack2.msi”安装完成之后解压“vsintlpack1.zip”,获得七个语言包。双击其中的“CHSPinYinConv.msi”完成安装后就可以在Visual Studio中引用了。
默认的语言包类库文件引用地址:“C:\Program Files (x86)\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library\ChnCharInfo.dll”
(把这两个dll引入到项目里面.可以写一个YinpinHelper.cs以供调用)
示例:
using引用命名空间:
using Microsoft.International.Converters.PinYinConverter;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.International.Converters.PinYinConverter; using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("汉字转全拼结果:" + GetPinyin("测试")); Console.WriteLine("汉字转全拼首字母结果:" + GetFirstPinyin("测试")); Console.WriteLine("简体转繁体结果:" + GetTraditionalChinese("测试")); Console.WriteLine("繁体转简体结果:" + GetSimplifiedChinese("測試")); Console.ReadLine(); } /// <summary> /// 汉字转化为拼音 /// </summary> /// <param name="str">汉字</param> /// <returns>全拼</returns> public static string GetPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, t.Length - 1); } catch { r += obj.ToString(); } } return r; } /// <summary> /// 汉字转化为拼音首字母 /// </summary> /// <param name="str">汉字</param> /// <returns>首字母</returns> public static string GetFirstPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0, 1); } catch { r += obj.ToString(); } } return r; } /// <summary> /// 简体中文转化为繁体中文 /// </summary> /// <param name="str">汉字</param> /// <returns>繁体中文</returns> public static string GetTraditionalChinese(string str) { if (string.IsNullOrWhiteSpace(str.Replace(" ", ""))) { return ""; } string result = null; char[] CharSimples = str.ToCharArray(); for (int i = 0; i < CharSimples.Length; i++) { if (ChineseChar.IsValidChar(CharSimples[i])) { result += ChineseConverter.Convert(CharSimples[i].ToString(), ChineseConversionDirection.SimplifiedToTraditional); } else { result += CharSimples[i].ToString(); } } return result; } /// <summary> /// 繁体中文转化为简体中文 /// </summary> /// <param name="str">汉字</param> /// <returns>简体中文</returns> public static string GetSimplifiedChinese(string str) { if (string.IsNullOrWhiteSpace(str.Replace(" ", ""))) { return ""; } string result = null; char[] CharSimples = str.ToCharArray(); for (int i = 0; i < CharSimples.Length; i++) { if (ChineseChar.IsValidChar(CharSimples[i])) { result += ChineseConverter.Convert(CharSimples[i].ToString(), ChineseConversionDirection.TraditionalToSimplified); } else { result += CharSimples[i].ToString(); } } return result; } } }
转: https://blog.csdn.net/qq395537505/article/details/51848681
http://t.zoukankan.com/gguozhenqian-p-4086466.html