using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace EdgeVoyager { public partial class Form2 : Form { string resxFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "favoritesMenuItem.resx"); public Form2() { InitializeComponent(); // 使用ResXResourceReader读取资源文件 using (ResXResourceReader reader = new ResXResourceReader(resxFilePath)) { // 遍历资源文件中的每一项 foreach (DictionaryEntry entry in reader) { // 将项的名称添加到ListBox中 listBox1.Items.Add(entry.Key.ToString()); } } ContextMenuStrip contextMenuStrip = new ContextMenuStrip(); ToolStripMenuItem deleteMenuItem = new ToolStripMenuItem("删除"); deleteMenuItem.Click += DeleteMenuItem_Click; contextMenuStrip.Items.Add(deleteMenuItem); listBox1.ContextMenuStrip = contextMenuStrip; } private void DeleteMenuItem_Click(object sender, EventArgs e) { ResXResourceReader reader = new ResXResourceReader(resxFilePath); // 获取选中的项 if (listBox1.SelectedItem != null) { string key = listBox1.SelectedItem.ToString(); ResXResourceWriter writer = new ResXResourceWriter(resxFilePath + ".temp"); bool itemFound = false; foreach (DictionaryEntry entry in reader) { if (entry.Key.ToString() != key) { writer.AddResource(entry.Key.ToString(), entry.Value); } else { itemFound = true; } } reader.Close(); writer.Generate(); writer.Close(); if (itemFound) { File.Delete(resxFilePath); File.Move(resxFilePath + ".temp", resxFilePath); } } listBox1.Items.Clear(); foreach (DictionaryEntry entry in reader) { listBox1.Items.Add(entry.Key.ToString()); } } } }