about 5 years ago

最近有個需要將手邊的程式檔案,轉為簡體字的

網路上有不少工具可以線上替你轉換,但檔案數量太多,如果一隻一隻上傳,不知道要傳到什麼時候

所以就自己想了一下,"寫一個可以將資料夾內的檔案轉換語言的"

功能1:選擇目的資料夾

選擇目的資料夾,並保存於文字方塊中
 private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtFolderPath.Text = folderBrowserDialog1.SelectedPath;
        }


撰寫類別提供使用

 public static class ChineseConverter
    {
        internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
        internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

        /// <summary> 
        /// 使用OS的kernel.dll做為簡繁轉換工具,只要有裝OS就可以使用,不用額外引用dll,但只能做逐字轉換,無法進行詞意的轉換 
        /// <para>所以無法將電腦轉成計算機</para> 
        /// </summary> 
        [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);

        /// <summary> 
        /// 繁體轉簡體 
        /// </summary> 
        /// <param name="pSource">要轉換的繁體字:體</param> 
        /// <returns>轉換後的簡體字:體</returns> 
        public static string ToSimplified(string pSource)
        {
            String tTarget = new String(' ', pSource.Length);
            int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
            return tTarget;
        }

        /// <summary> 
        /// 簡體轉繁體 
        /// </summary> 
        /// <param name="pSource">要轉換的繁體字:體</param> 
        /// <returns>轉換後的簡體字:體</returns> 
        public static string ToTraditional(string pSource)
        {
            String tTarget = new String(' ', pSource.Length);
            int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
            return tTarget;
        }
        ///
        /// 使用 Office Word (Microsoft.Office.Interop.Word) 進行轉換
        ///
        public static string ConvertUsingWord(string argSource, bool argIsCht)
        {
            var doc = new Document();
            doc.Content.Text = argSource;
            doc.Content.TCSCConverter(
                argIsCht
                    ? WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC
                    : WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
            var ret = doc.Content.Text;
            object saveChanges = false;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;
            doc.Close(ref saveChanges, ref originalFormat, ref routeDocument);
            return ret;
        }
    }

選擇資料夾轉換

 private void btnCovertFolder_Click(object sender, EventArgs e)
        {
            string src = txtFolderPath.Text; //來源資料夾
            string target = txtTargetPath.Text; //目的資料夾
            
            foreach(string fileName  in Directory.GetFiles(src, "*", SearchOption.AllDirectories))
            {
                FileInfo fi = new FileInfo(fileName);
                string targetPath = target + @"\" + fi.Name;
                //if (!File.Exists(target + @"\" + fi.Name))
                //{
                //    File.Create(target + @"\" + fi.Name);
                //}

                Stream fileStream = File.OpenRead(fileName);
                string result = string.Empty;
                using (StreamReader reader = new StreamReader(fileStream, Encoding.GetEncoding("BIG5")))
                {
                    // Read the first line from the file and write it the textbox.
                     result = ChineseConverter.ToSimplified(reader.ReadToEnd());
                }
                Stream targetFile = File.Open(targetPath, FileMode.OpenOrCreate);
                using(StreamWriter sw = new StreamWriter(targetFile,Encoding.GetEncoding("GB2312")))
                {
                    sw.Write(result);
                }
                targetFile.Close();
                fileStream.Close();
            }
        }

所以用這種方法基本上是非常簡單的,那選單檔的當然也就是一樣的意思,然後判斷文本的檔案型態ANSI要轉換成BIG5 or GB2312

private void btnSelectFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            Stream fileStream = openFileDialog1.OpenFile();
            string fileFormate = (comboBox1.SelectedItem.ToString()=="ANSI")?"BIG5":"UTF-8";

            using (StreamReader reader = new StreamReader(fileStream, Encoding.GetEncoding(fileFormate)))
            {
                // Read the first line from the file and write it the textbox.
                txtFileResult.Text = reader.ReadToEnd();
                if (chkFileUsingWord.Checked) 
                    txtFileResult.Text = ChineseConverter.ConvertUsingWord(txtFileResult.Text,true);
                else
                    txtFileResult.Text = ChineseConverter.ToSimplified(txtFileResult.Text);

            }
            fileStream.Close();
        }

文字互轉

  private void btnTtoS_Click(object sender, EventArgs e)
        {
            string src =  txtTaget.Text.Trim();
           txtResult.Text = ChineseConverter.ToSimplified(src);           
        }

        private void btnStoT_Click(object sender, EventArgs e)
        {
            string src = txtTaget.Text.Trim();
            txtResult.Text = ChineseConverter.ToTraditional(src);  
        }

        private void btnTransWord_Click(object sender, EventArgs e)
        {
            string src = txtTaget.Text.Trim();
            txtResult.Text = ChineseConverter.ConvertUsingWord(src,true);  
        }

https://github.com/CowellJackli/ChangeEncodeTool

← JSON檔無法透過URL開啟 Classic ASP 網址中文URL處理方式 →
 
comments powered by Disqus