代码大多来源于网络
开发工具: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() + " 个文件"; } } } }