上下文菜单项未显示

时间:2013-11-27 12:04:25

标签: c# .net winforms winapi registry

我正在尝试在资源管理器上下文菜单中放置一个新的菜单项,但我无法让它工作。

我没有收到任何异常或错误消息,我设置了断点,但它们没有被击中。我已经搜索了注册表,它不在那里。我做错了什么?

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

1 个答案:

答案 0 :(得分:5)

以管理员身份运行您的应用。 更改注册表可能需要管理员权限。