C# winForm utf8 gbk 相互轉碼小工具
阿新 • • 發佈:2018-12-25
程式碼大多來源於網路
開發工具:vs2017
專案檔案:
連結:https://pan.baidu.com/s/1OVrAL5DjhDJEJGVAX7m29Q
提取碼:67lj
介面:
程式碼:
using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; namespace UTF8_GBK_TransCode { public partial class Form1 : Form {public Form1() { InitializeComponent(); CmbType.SelectedIndex = 0;//顯示預設轉換型別 } //選擇目標資料夾按鈕 private void BtnSeleteTargetFolder_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { TxbTargetFolder.Text= folderBrowserDialog1.SelectedPath; } } //目標資料夾路徑改變時 private void TxbTargetFolder_TextChanged(object sender, EventArgs e) { showList(); } //格式文字框內容改變時 private void TxbFileExt_TextChanged(object sender, EventArgs e) { showList(); }//顯示檔案列表 public void showList() { if (TxbTargetFolder.Text.Trim() != "") { //初始化檔案列表 CheckedListBoxFiles.Items.Clear(); if (Directory.Exists(TxbTargetFolder.Text.Trim()))//如果資料夾存在 { try { GetDirectory(TxbTargetFolder.Text.Trim());//遍歷目錄下所有檔案 } catch (Exception ex) { MessageBox.Show("獲取原始檔內檔案列表失敗:" + ex.Message); } } else if (File.Exists(TxbTargetFolder.Text.Trim()))//如果檔案存在 { FileInfo f = new FileInfo(TxbTargetFolder.Text.Trim()); Regex r = new Regex(f.Extension); Match m = r.Match(TxbFileExt.Text.Trim()); if (m.Success) {//過濾檔案格式 int index = CheckedListBoxFiles.Items.Add(TxbTargetFolder.Text.Trim());//新增listbox,顯示檔案 CheckedListBoxFiles.SetItemChecked(index, true);//預設勾選新增項 } } } } //獲得指定路徑下所有子目錄名 public void GetDirectory(string path) { GetFileName(path); DirectoryInfo root = new DirectoryInfo(path);//目錄 foreach (DirectoryInfo d in root.GetDirectories())//遍歷目錄 { GetDirectory(d.FullName); } } //獲得指定路徑下所有檔名 public void GetFileName(string path) { DirectoryInfo root = new DirectoryInfo(path);//目錄 foreach (FileInfo f in root.GetFiles()) { Regex r = new Regex(f.Extension); Match m = r.Match(TxbFileExt.Text.Trim()); if(m.Success)//過濾檔案格式 { int index=CheckedListBoxFiles.Items.Add(f.FullName);//新增listbox,顯示檔案 CheckedListBoxFiles.SetItemChecked(index, true);//預設勾選新增項 toolStripStatusLabel1.Text = "選中 " +CheckedListBoxFiles.Items.Count.ToString()+ " 個檔案";//顯示檔案個數 } } } //拖動資料夾進入檔案路徑文字框時 private void TxbTargetFolder_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop))//如果是檔案型別 { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } //拖動資料夾到檔案路徑文字框後 private void TxbTargetFolder_DragDrop(object sender, DragEventArgs e) { TxbTargetFolder.Text = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();//獲取檔案|資料夾路徑 } //開始轉碼 private void BtnStartTransCode_Click(object sender, EventArgs e) { if (CheckedListBoxFiles.CheckedItems.Count > 0)//如果有選中的檔案 { int coverSuccess = 0;//轉換成功數 if(CmbType.SelectedIndex == 0)//GBK 轉 UTF8 { try{ for (int i = 0; i < CheckedListBoxFiles.CheckedItems.Count; i++)//遍歷選中項 { GBKtoUTF8(CheckedListBoxFiles.CheckedItems[i].ToString());//呼叫轉碼函式 coverSuccess++; } } catch (Exception ex) { toolStripStatusLabel1.Text = ex.Message; } finally { toolStripStatusLabel1.Text = "轉碼成功檔案: " + coverSuccess.ToString() + " 個"; } } else if(CmbType.SelectedIndex == 1)//UTF8 轉 GBK { try { for (int i = 0; i < CheckedListBoxFiles.CheckedItems.Count; i++)//遍歷選中項 { UTF8toGBK(CheckedListBoxFiles.CheckedItems[i].ToString());//呼叫轉碼函式 coverSuccess++; } } catch (Exception ex) { toolStripStatusLabel1.Text = ex.Message; } finally { toolStripStatusLabel1.Text = "轉碼成功檔案: " + coverSuccess.ToString() + " 個"; } } } } public void GBKtoUTF8(string filePath)//GBK 轉 UTF8 { var buffer = File.ReadAllBytes(filePath);//讀取檔案內容 buffer = Encoding.Convert(Encoding.GetEncoding("gbk"), Encoding.UTF8, buffer);//轉碼 File.WriteAllBytes(filePath, buffer);//寫入檔案 toolStripStatusLabel1.Text = "轉碼成功"; } public void UTF8toGBK(string filePath)//UTF8 轉 GBK { var buffer = File.ReadAllBytes(filePath);//讀取檔案內容 buffer = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("gbk"), buffer);//轉碼 File.WriteAllBytes(filePath, buffer);//寫入檔案 } //全選按鈕 private void BtnSelectAll_Click(object sender, EventArgs e) { int fileItems = CheckedListBoxFiles.Items.Count; if (fileItems > 0) { for(int i = 0; i < fileItems; i++) { CheckedListBoxFiles.SetItemChecked(i,true); } } } //反選按鈕 private void BtnSelectInvert_Click(object sender, EventArgs e) { int fileItems = CheckedListBoxFiles.Items.Count; if (fileItems > 0) { for (int i = 0; i < fileItems; i++) { if (CheckedListBoxFiles.GetItemChecked(i)) { CheckedListBoxFiles.SetItemChecked(i, false); } else { CheckedListBoxFiles.SetItemChecked(i, true); } } } } //檔案列表中的項改變選中狀態時 private void CheckedListBoxFiles_ItemCheck(object sender, ItemCheckEventArgs e) { // 這個事件是指示某項的選中狀態將要被更改、在更改前會執行這個事件 // 所以checkedListBox.CheckedItems.Count 獲取的是更改之前的值 if (e.NewValue == CheckState.Checked)//選中 { toolStripStatusLabel1.Text = "選中 " + (CheckedListBoxFiles.CheckedItems.Count + 1).ToString() + " 個檔案"; } else//非選中 { toolStripStatusLabel1.Text = "選中 " + (CheckedListBoxFiles.CheckedItems.Count - 1).ToString() + " 個檔案"; } } } }