12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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());
- }
- }
- }
- }
|