65.9K
CodeProject 正在变化。 阅读更多。
Home

向 Windows 资源管理器添加上下文菜单

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (35投票s)

2005年4月12日

1分钟阅读

viewsIcon

375871

downloadIcon

13145

仅使用注册表为 Windows Explorer 添加右键菜单。

引言

我们许多人喜欢 Windows Explorer 中右键菜单的便利性,并且讨厌在命令提示符窗口中输入长路径。此工具将通过在右键单击文件夹时在右键菜单上添加新项目来解决此问题。您可以获得一个将在父目录中打开的命令提示符,而不会在其他地方打开。如果您对命令提示符不感兴趣,那么您可以指定您选择的另一个可执行文件作为目标,仍然可以节省一些点击和输入。

背景

处理 Explorer 通常需要一些 shell 编程,这并不总是很理想。幸运的是,我将描述的技巧仅仅是设置一些注册表项来实现这一点。

使用代码

注册表项如下

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

代码不言自明,它只是设置注册表

private void btnAddMenu_Click(object sender, System.EventArgs e)
{
    RegistryKey regmenu = null;
    RegistryKey regcmd = null;
    try
    {
        regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
        if(regmenu != null)
            regmenu.SetValue("",this.txtName.Text);
        regcmd = Registry.ClassesRoot.CreateSubKey(Command);
        if(regcmd != null)
                regcmd.SetValue("",this.txtPath.Text);
    }
    catch(Exception ex)
    {
        MessageBox.Show(this,ex.ToString());
    }
    finally       
    {
        if(regmenu != null)
            regmenu.Close();
        if(regcmd != null)
            regcmd.Close();
    }        
}

或者,如果您不再喜欢它们,则删除这些设置

private void btnRemoveMenu_Click(object sender, System.EventArgs e)
{
    try
    {
        RegistryKey reg = Registry.ClassesRoot.OpenSubKey(Command);
        if(reg != null)
        {
            reg.Close();
            Registry.ClassesRoot.DeleteSubKey(Command);
        }
        reg = Registry.ClassesRoot.OpenSubKey(MenuName);
        if(reg != null)
        {
            reg.Close();
            Registry.ClassesRoot.DeleteSubKey(MenuName);
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(this,ex.ToString());
    }
}

代码的其余部分仅提供一个合理的用户界面。

关注点

您显然拥有更改注册表的权限,以便该应用程序按预期运行。

历史

  • 2005 年 4 月 12 日 - 版本 1.0.0.0。
© . All rights reserved.