MainForm.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. namespace FileNameCharsetConvert
  6. {
  7. public partial class MainForm : Form
  8. {
  9. public MainForm()
  10. {
  11. InitializeComponent();
  12. cmbEncoding.DataSource = Encoding.GetEncodings();
  13. }
  14. private void btnSelectFolder_Click(object sender, EventArgs e)
  15. {
  16. if (dlgFolder.ShowDialog() == DialogResult.OK)
  17. {
  18. txtFolder.Text = dlgFolder.SelectedPath;
  19. }
  20. }
  21. private void btnScan_Click(object sender, EventArgs e)
  22. {
  23. lvMain.Items.Clear();
  24. var files = Directory.GetFiles(txtFolder.Text, "*.*", System.IO.SearchOption.AllDirectories);
  25. for (var i = 0; i < files.Length; i++)
  26. {
  27. proc.Value = (int)(i * 1.0f / files.Length * 100);
  28. var lvi = new ListViewItem { Text = files[i] };
  29. lvi.SubItems.Add(" ");
  30. lvi.SubItems.Add("未转换");
  31. lvMain.Items.Add(lvi);
  32. }
  33. lvces();
  34. proc.Value = 100;
  35. }
  36. private void btnConvert_Click(object sender, EventArgs e)
  37. {
  38. var enci = (EncodingInfo)cmbEncoding.SelectedItem;
  39. var enc = enci.GetEncoding();
  40. var def = Encoding.Default;
  41. for (var i = 0; i < lvMain.Items.Count; i++)
  42. {
  43. proc.Value = (int)(i * 1.0f / lvMain.Items.Count * 100);
  44. var item = lvMain.Items[i];
  45. item.SubItems[1].Text = enc.GetString(def.GetBytes(item.Text));
  46. item.SubItems[2].Text = "已转换,未应用";
  47. }
  48. lvces();
  49. }
  50. private void lvces()
  51. {
  52. foreach (ColumnHeader item in lvMain.Columns)
  53. {
  54. item.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
  55. }
  56. }
  57. private void btnApplySelection_Click(object sender, EventArgs e)
  58. {
  59. for (var i = 0; i < lvMain.SelectedItems.Count; i++)
  60. {
  61. var lvi = lvMain.SelectedItems[i];
  62. try
  63. {
  64. File.Move(lvi.Text, lvi.SubItems[1].Text);
  65. lvi.SubItems[2].Text = "成功应用";
  66. }
  67. catch (Exception ex)
  68. {
  69. lvi.SubItems[2].Text = "应用失败:" + ex.Message;
  70. }
  71. }
  72. }
  73. private void txtFolder_DragEnter(object sender, DragEventArgs e)
  74. {
  75. string[] files;
  76. e.Effect =
  77. e.Data.GetDataPresent(DataFormats.FileDrop) && (files = (string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1 && Directory.Exists(files[0])
  78. ? DragDropEffects.Link
  79. : DragDropEffects.None;
  80. }
  81. private void txtFolder_DragDrop(object sender, DragEventArgs e)
  82. {
  83. txtFolder.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
  84. }
  85. }
  86. }