MainForm.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Diagnostics;
  11. namespace FileCharsetConvert;
  12. public partial class MainForm : Form
  13. {
  14. public MainForm()
  15. {
  16. InitializeComponent();
  17. cmbInEnc.DataSource = Encoding.GetEncodings();
  18. cmbOutEnc.DataSource = Encoding.GetEncodings();
  19. }
  20. private void btnSelectFolder_Click(object sender, EventArgs e)
  21. {
  22. if (dlgFolder.ShowDialog() == DialogResult.OK)
  23. txtFolder.Text = dlgFolder.SelectedPath;
  24. }
  25. private void btnScan_Click(object sender, EventArgs e)
  26. {
  27. List<string> ScanResult = new List<string>();
  28. foreach (var item in txtFlt.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
  29. {
  30. ScanResult.AddRange(Directory.GetFiles(txtFolder.Text, item,
  31. chkSubDir.Checked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly));
  32. }
  33. var FileList = ScanResult.Distinct().ToList();
  34. lvMain.Items.Clear();
  35. foreach (var item in FileList)
  36. {
  37. var lvi = new ListViewItem();
  38. lvi.Text = item;
  39. lvi.SubItems.Add("未转换");
  40. lvMain.Items.Add(lvi);
  41. }
  42. }
  43. private void btnApplySelection_Click(object sender, EventArgs e)
  44. {
  45. foreach (ListViewItem item in lvMain.SelectedItems)
  46. {
  47. File.WriteAllText(item.Text,
  48. File.ReadAllText(item.Text, ((EncodingInfo)cmbInEnc.SelectedItem).GetEncoding()),
  49. ((EncodingInfo)cmbOutEnc.SelectedItem).GetEncoding());
  50. item.SubItems[1].Text = "已转换";
  51. }
  52. }
  53. private void lvMain_MouseDoubleClick(object sender, MouseEventArgs e)
  54. {
  55. if (lvMain.SelectedItems.Count != 0)
  56. {
  57. string filename = Path.GetTempFileName() + "-" + Path.GetFileName(lvMain.SelectedItems[0].Text) + ".txt";
  58. File.WriteAllText(filename,
  59. File.ReadAllText(lvMain.SelectedItems[0].Text, ((EncodingInfo)cmbInEnc.SelectedItem).GetEncoding()),
  60. ((EncodingInfo)cmbOutEnc.SelectedItem).GetEncoding());
  61. Process.Start(new ProcessStartInfo() { FileName = filename, UseShellExecute = true });
  62. }
  63. }
  64. }