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つの解決策があります。1つは、レジストリHKEY_ CLASSES_ ROOTに作成されましたが、ドキュメントファイルを右クリックしても結果が表示されません。

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

Explorerのコンテキストメニューに項目を追加したいと思います。これを行う方法を示すCodeProjectに関する素晴らしい記事があります:http: //www.codeproject.com/KB/cs/appendmenu.aspx(基本的にはWindowsレジストリに適切なキーを追加するだけです)

于 2009-09-28T03:36:02.850 に答える
0

.docファイルのファイルタイプ(ProgID)を確認する必要があります。これはで見つけることができますHKEY_CURRENT_USER\Software\Classes\.doc(これはデフォルト値です)。

次に、キーを追加します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

ご回答ありがとうございました。非常に2はそれらを高く評価します。

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 に答える