Form2.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Resources;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace EdgeVoyager
  15. {
  16. public partial class Form2 : Form
  17. {
  18. string resxFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "favoritesMenuItem.resx");
  19. public Form2()
  20. {
  21. InitializeComponent();
  22. // 使用ResXResourceReader读取资源文件
  23. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  24. {
  25. // 遍历资源文件中的每一项
  26. foreach (DictionaryEntry entry in reader)
  27. {
  28. // 将项的名称添加到ListBox中
  29. listBox1.Items.Add(entry.Key.ToString());
  30. }
  31. }
  32. ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
  33. ToolStripMenuItem deleteMenuItem = new ToolStripMenuItem("删除");
  34. deleteMenuItem.Click += DeleteMenuItem_Click;
  35. contextMenuStrip.Items.Add(deleteMenuItem);
  36. listBox1.ContextMenuStrip = contextMenuStrip;
  37. }
  38. private void DeleteMenuItem_Click(object sender, EventArgs e)
  39. {
  40. ResXResourceReader reader = new ResXResourceReader(resxFilePath);
  41. // 获取选中的项
  42. if (listBox1.SelectedItem != null)
  43. {
  44. string key = listBox1.SelectedItem.ToString();
  45. ResXResourceWriter writer = new ResXResourceWriter(resxFilePath + ".temp");
  46. bool itemFound = false;
  47. foreach (DictionaryEntry entry in reader)
  48. {
  49. if (entry.Key.ToString() != key)
  50. {
  51. writer.AddResource(entry.Key.ToString(), entry.Value);
  52. }
  53. else
  54. {
  55. itemFound = true;
  56. }
  57. }
  58. reader.Close();
  59. writer.Generate();
  60. writer.Close();
  61. if (itemFound)
  62. {
  63. File.Delete(resxFilePath);
  64. File.Move(resxFilePath + ".temp", resxFilePath);
  65. }
  66. }
  67. listBox1.Items.Clear();
  68. foreach (DictionaryEntry entry in reader)
  69. {
  70. listBox1.Items.Add(entry.Key.ToString());
  71. }
  72. }
  73. }
  74. }