1

简而言之,我的问题是如何实现“发送到”(右键单击 Windows 上的文件)

我有一个数据网格视图,其中还包含一个带有日志文件名的列(我知道每个文件的路径)

我想在弹出菜单中添加复制选项到桌面和密钥(可移动)驱动程序的磁盘。

我的弹出菜单可能如下所示:

   View log

   Open file location

   <---------------->

   Copy to -->  Desktop
                (and Removable Drivers)

   ...

所以我想要:

  1. 在“复制到”子菜单下添加带有“桌面”和所有可移动驱动程序的列表(并删除用户弹出的可移动驱动程序)

  2. 正如我所说我想将文件复制到可移动驱动程序,那么如何添加“动态事件” - 我的意思是 - 如果用户插入 4 个 Disk On Key 驱动程序,我在“复制到”子菜单下有新的 4 行(比方说,桌面和 E:\、F:\、G:\、L:),所以我需要每个可移动驱动程序的新点击事件来将文件复制到真正的驱动程序......

关于问题 1 - 我找到了检测可移动驱动程序是否插入计算机的代码,并且我成功地将可移动驱动程序添加到子菜单中。但我没有成功从子菜单中删除项目:

private void menu_PopUp_Opening(object sender, CancelEventArgs e)
{
    // Need to remove all removable drivers first --> How to do ?

    // to update the USB drivers when opening new pop up menu
    DriveInfo[] ListDrives = DriveInfo.GetDrives();
    foreach (DriveInfo Drive in ListDrives)
    {
        if (Drive.DriveType == DriveType.Removable)
        {
            // add to popup menu, from: http://stackoverflow.com/questions/5868446/how-to-add-sub-menu-items-in-contextmenustrip-using-c4-0
            (menu_PopUp.Items[3] as ToolStripMenuItem).DropDownItems.Add(Drive.Name + " (" + Drive.VolumeLabel + ")");
        }
    }
}

谢谢你的帮助!

4

2 回答 2

2

为什么不直接删除除顶部菜单项之外的所有菜单子项,即“桌面”:

  ...
  // Need to remove all removable drivers first
  ToolStripMenuItem copyToItem = menu_PopUp.Items[3] as ToolStripMenuItem;

  // Assuming that "Desktop" menu item is the top one, 
  // we should delete all the items except #0 
  for (int i = copyToItem.DropDownItems.Count - 1; i >= 1; --i)
    copyToItem.DropDownItems.RemoveAt(i);

  ...
  // to update the USB drivers when opening new pop up menu
  DriveInfo[] ListDrives = DriveInfo.GetDrives();

  foreach (DriveInfo Drive in ListDrives) {
    if (Drive.DriveType == DriveType.Removable) {
      // add to popup menu, from: http://stackoverflow.com/questions/5868446/how-to-add-sub-menu-items-in-contextmenustrip-using-c4-0
      ToolStripItem item = (menu_PopUp.Items[3] as ToolStripMenuItem).DropDownItems.Add(Drive.Name + " (" + Drive.VolumeLabel + ")");

      item.Tag = Drive.Name; // <- bind (via tag) driver name with menu item
      item.Click += OnRemovableDriveClick;
    }
  }

...

  private void OnRemovableDriveClick(object sender, EventArgs e) {
    ToolStripItem item = sender as ToolStripItem;

    String driveName = item.Tag as String;
    ...
于 2013-07-29T06:02:49.257 回答
0
  1. 获取对“复制到”项目的引用
  2. 遍历该项目的项目

    ToolStripMenuItem copyToItem = menuStrip.Item(...)

    foreach(copyToItem.Items 中的 ToolStripMenuItem 项){ copyToItem.Items.Remove(item); }

于 2013-07-29T05:52:39.217 回答