右键单击时为我的程序添加快捷方式

时间:2009-09-28 03:24:34

标签: windows registry regedit

我不确定应该叫什么确切的术语。我想在Windows中右键单击时为我的C#程序添加快捷方式。

根据我的发现,它与配置“regedit”有关。我有这个例子,但它是为IE制作的。任何人都可以指出任何可以解决我的问题的参考资料吗?

的引用:

http://blog.voidnish.com/?p=17 http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/

非常感谢你。

今天更新..

根据Factor Mystic的回复,我将此代码添加到原始代码中。我有2个解决方案。一,它是在注册表 HKEY_ CLASSES_ ROOT 中创建的,但是当我右键单击doc文件时,我看不到结果。

private const string ProgName = "Software\\Classes\\Word.Document\\shell";
private const string MenuName = "Software\\Classes\\Word.Document\\shell\\NewTesting";
public const string Command =Software\\Classes\\Word.Document\\shell\\NewTesting\\command";

    private void Form1_Load(object sender, EventArgs e)
    {
        txtProgram.Text = "Word.Document.8";
        txtName.Text = "Testing";
        txtPath.Text = "C:\\temp\\encriptTest.exe";
        check();
        addItem()
    }
    public void check()
    {
        RegistryKey regmenu = null;
        RegistryKey regcmd = null;
        try
        {
            //this.CheckSecurity();
            regmenu = Registry.ClassesRoot.OpenSubKey(MenuName, false);
        }
        catch (ArgumentException ex)
        {
            // RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList.
            MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess.  "
                + "AllAccess cannot be used as a parameter in GetPathList because it represents more than one "
                + "type of registry variable access : \n" + ex);
        }
        catch (SecurityException ex)
        {
            // RegistryPermissionAccess.AllAccess can not be used as a parameter for GetPathList.
            MessageBox.Show(this, "An ArgumentException occured as a result of using AllAccess.  " + ex);
            this.btnAddMenu.Enabled = false;
            //this.btnRemoveMenu.Enabled = false;
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, ex.ToString());
        }
        finally
        {
            if (regmenu != null)
                regmenu.Close();
            if (regcmd != null)
                regcmd.Close();
        }
    }

    private void CheckSecurity()
    {
        //check registry permissions
        RegistryPermission regPerm;
        regPerm = new RegistryPermission(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + ProgName);
        regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + MenuName);
        regPerm.AddPathList(RegistryPermissionAccess.Write, "HKEY_CLASSES_ROOT\\" + Command);
        regPerm.Demand();
    }

    private void addItem()
    {
        RegistryKey regmenu = null;
        RegistryKey regcmd = null;
        RegistryKey regprog = null;
        try
        {
            regprog = Registry.ClassesRoot.CreateSubKey(ProgName);
            if (regmenu != null)
                regmenu.SetValue("", this.txtProgram.Text);
            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 (regprog != null)
                regprog.Close();
            if (regmenu != null)
                regmenu.Close();
            if (regcmd != null)
                regcmd.Close();
        }       
    }

其次,在HKEY_ LOCAL_ MACHINE中创建。

private bool Add_Item(string Extension,string MenuName, string MenuDescription, string MenuCommand)
    {
        //receive .doc,OpenTest,Open with Opentest,path: C:\\temp\\encriptTest.exe %1
        bool ret = false;
        RegistryKey rkey = //receive .doc extension (word.Document.8)
          Registry.ClassesRoot.OpenSubKey(Extension); //set HKEY_LOCAL_MACHINE\software\classes\word.Document.8 
        if (rkey != null)
        {
            string extstring = rkey.GetValue("").ToString();
            rkey.Close();
            if (extstring != null)
            {
                if (extstring.Length > 0)
                {
                    rkey = Registry.ClassesRoot.OpenSubKey(extstring, true);
                    if (rkey != null) //with extension file receive OpenTest as shell
                    {
                        string strkey = "shell\\" + MenuName + "\\command"; //..\shell\OpenTest\command
                        RegistryKey subky = rkey.CreateSubKey(strkey);
                        if (subky != null)
                        {
                            subky.SetValue("", MenuCommand); // path: C:\\temp\\encriptTest.exe %1
                            subky.Close();
                            subky = rkey.OpenSubKey("shell\\" + MenuName, true); //..\shell\OpenTest
                            if (subky != null)
                            {
                                subky.SetValue("", MenuDescription); // name displayed: Open with &OpenTest
                                subky.Close();
                            }
                            ret = true;
                        }
                        rkey.Close();
                    }
                }
            }
        }
        return ret;
    }
}

我担心,我应该使用哪个主键?

3 个答案:

答案 0 :(得分:0)

我相信您要将项目添加到资源管理器上下文菜单中。 这是关于CodeProject的一篇很好的文章,它向您展示了如何做到这一点: http://www.codeproject.com/KB/cs/appendmenu.aspx(基本上只是将相应的密钥添加到Windows注册表中)

答案 1 :(得分:0)

您将要确定.doc文件的文件类型(ProgID)。您可以在HKEY_CURRENT_USER\Software\Classes\.doc中找到它(这是默认值)。

然后添加密钥HKEY_CURRENT_USER\Software\Classes\<ProgID>\shell\NewMenuOption\command,其中默认值是程序的路径。

您可以使用Registry.SetValueGetValue完成所有这些操作。

Check out this msdn page to get started.

编辑:其他信息,蜂巢键之间的区别:

HKEY_LOCAL_MACHINE\Software\ClassesHKEY_CURRENT_USER\Software\Classes类似,但HKLM用于系统默认值/所有用户设置,HKCU用于每个用户设置。每个用户设置不需要提升权限,因此您可以将您的上下文菜单键编写为普通用户而不会感到痛苦。

HKEY_CLASSES_ROOT是一个组合HKEY_LOCAL_MACHINE\Software\ClassesHKEY_CURRENT_USER\Software\Classes的视图,其中包含针对HKLM的写入。这是编写系统默认值的快捷方式,许多教程都会显示这一点,因为它的略微更简单,但除非您为所有用户安装应用程序,否则我不推荐它。

Advanced registry info on MSDN

答案 2 :(得分:0)

非常感谢您的回复。 Very2 apreciate Them ..

作为Per Conlcusion,解决我的问题的3种方法。简单易懂的方法:

以3种方式添加快捷方式:

<强> 1。直接在注册表窗口中创建:    http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/

<强> 2。快捷方式仅适用于文件夹。    http://www.codeproject.com/KB/cs/appendmenu.aspx    http://blog.voidnish.com/?p=17

第3。可用于所有文件和文件夹的快捷方式。    http://www.autoitscript.com/forum/index.php?showtopic=103265&view=findpost&p=731920

相关问题