代碼大多來源於網絡網絡
開發工具:vs2017函數
項目文件:工具
連接: https://pan.baidu.com/s/1JIcCo_ZNcsw1U2Z6pB5s_g開發工具
提取碼: ecjkspa
界面:code
代碼:orm
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() + " 個文件"; } } } }