简体   繁体   中英

Context Menu item not showing

I am trying to place a new Menuitem in Explorer context menu and I cannot get it to work.

I am not getting any exceptions or error messages and I've set breakpoints and they're not being hit. And I've searched the registry and it's not there. What am I doing wrong?

    private const string MenuName = "Folder\\shell\\NewMenuOption";
    private const string Command = "Folder\\shell\\NewMenuOption\\command";

    private void Form1_Shown(object sender, EventArgs e)
    {
        using(var folder = new FolderBrowserDialog())
        {
            if(folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.ArchivePath = folder.SelectedPath;
                Properties.Settings.Default.Save();

                RegistryKey regmenu = null;
                RegistryKey regcmd = null;
                try
                {
                    regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
                    if (regmenu != null)
                        regmenu.SetValue("", "Archive");
                    regcmd = Registry.ClassesRoot.CreateSubKey(Command);
                    if (regcmd != null)
                        regcmd.SetValue("", Environment.CurrentDirectory + @"\Archiver.exe");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.ToString());
                }
                finally
                {
                    if (regmenu != null)
                        regmenu.Close();
                    if (regcmd != null)
                        regcmd.Close();
                }
            }
            else
            {
                if(MessageBox.Show("In order to use Archiver, you must first specify where your archive is. Do you want to continue?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes)
                {
                    Application.Restart();
                }
                else
                {
                    this.Dispose(true);
                }
            }
        }
    }

Run your app as admin. Making changes to the register may require admin rights.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM