123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.IO;
- using System.Text;
- using System.Windows.Forms;
- namespace FileNameCharsetConvert
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- cmbEncoding.DataSource = Encoding.GetEncodings();
- }
- private void btnSelectFolder_Click(object sender, EventArgs e)
- {
- if (dlgFolder.ShowDialog() == DialogResult.OK)
- {
- txtFolder.Text = dlgFolder.SelectedPath;
- }
- }
- private void btnScan_Click(object sender, EventArgs e)
- {
- lvMain.Items.Clear();
- var files = Directory.GetFiles(txtFolder.Text, "*.*", System.IO.SearchOption.AllDirectories);
- for (var i = 0; i < files.Length; i++)
- {
- proc.Value = (int)(i * 1.0f / files.Length * 100);
- var lvi = new ListViewItem { Text = files[i] };
- lvi.SubItems.Add(" ");
- lvi.SubItems.Add("未转换");
- lvMain.Items.Add(lvi);
- }
- lvces();
- proc.Value = 100;
- }
- private void btnConvert_Click(object sender, EventArgs e)
- {
- var enci = (EncodingInfo)cmbEncoding.SelectedItem;
- var enc = enci.GetEncoding();
- var def = Encoding.Default;
- for (var i = 0; i < lvMain.Items.Count; i++)
- {
- proc.Value = (int)(i * 1.0f / lvMain.Items.Count * 100);
- var item = lvMain.Items[i];
- item.SubItems[1].Text = enc.GetString(def.GetBytes(item.Text));
- item.SubItems[2].Text = "已转换,未应用";
- }
- lvces();
- }
- private void lvces()
- {
- foreach (ColumnHeader item in lvMain.Columns)
- {
- item.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
- }
- }
- private void btnApplySelection_Click(object sender, EventArgs e)
- {
- for (var i = 0; i < lvMain.SelectedItems.Count; i++)
- {
- var lvi = lvMain.SelectedItems[i];
- try
- {
- File.Move(lvi.Text, lvi.SubItems[1].Text);
- lvi.SubItems[2].Text = "成功应用";
- }
- catch (Exception ex)
- {
- lvi.SubItems[2].Text = "应用失败:" + ex.Message;
- }
- }
- }
- private void txtFolder_DragEnter(object sender, DragEventArgs e)
- {
- string[] files;
- e.Effect =
- e.Data.GetDataPresent(DataFormats.FileDrop) && (files = (string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1 && Directory.Exists(files[0])
- ? DragDropEffects.Link
- : DragDropEffects.None;
- }
- private void txtFolder_DragDrop(object sender, DragEventArgs e)
- {
- txtFolder.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
- }
- }
- }
|