0

我不确定我应该叫什么确切的术语。当我在 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;
    }
}

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

4

3 回答 3

0

您需要确定 .doc 文件的文件类型 (ProgID)。你可以在HKEY_CURRENT_USER\Software\Classes\.doc(这是默认值)。

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

您可以使用Registry.SetValue和完成所有这些操作GetValue

查看此 msdn 页面以开始使用。

编辑:附加信息,蜂巢键之间的区别:

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

HKEY_CLASSES_ROOT是一个结合HKEY_LOCAL_MACHINE\Software\Classes和的视图HKEY_CURRENT_USER\Software\Classes,写入指向 HKLM。这是编写系统默认值的快捷方式,许多教程都显示了这一点,因为它有点简单一些,但除非您为所有用户安装应用程序,否则我不推荐它。

MSDN 上的高级注册表信息

于 2009-09-28T16:33:49.210 回答
0

非常感谢您的回复。非常感谢他们..

根据 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

于 2009-10-05T02:36:00.580 回答
0

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

于 2009-09-28T03:36:02.850 回答