views:

133

answers:

3

Hi all,

Im not sure what the exact term should i called. I want to add shortcut to my C# program when i right click in windows.

From my findings, it got something to do with configure the "regedit". I have this example, but it was made for IE. can anyone point me to any references that can solve my problems?

references:

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

thank you very much.

UPDATED today..

Based on response from Factor Mystic, i add this code to the original. I have 2 solutions. One, It was created in registry HKEY_ CLASSES_ ROOT, but i cannot see the result when i right click the doc files.

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

Second, create in 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;
    }
}

My concerned, which Main Key should i use?

A: 

I believe you want to add items to the Explorer context menu. Here is a nice article on CodeProject that shows you how to do it: http://www.codeproject.com/KB/cs/appendmenu.aspx (basically it's just adding the appropriate keys to the windows registry)

Tom Frey
A: 

You're going to want to determine the file type (ProgID) of .doc files. You can find this in HKEY_CURRENT_USER\Software\Classes\.doc (it is the default value).

Then add the key HKEY_CURRENT_USER\Software\Classes\<ProgID>\shell\NewMenuOption\command, where the default value is the path to your program.

You can do all this with Registry.SetValue and GetValue.

Check out this msdn page to get started.

Edit: Additional info, the difference between hive keys:

HKEY_LOCAL_MACHINE\Software\Classes and HKEY_CURRENT_USER\Software\Classes are similar, but HKLM is for system defaults/all user settings, and HKCU is for per user settings. Per user settings don't require elevated privileges, so you can write your context menu keys as a regular user with no pain.

HKEY_CLASSES_ROOT is a view combining HKEY_LOCAL_MACHINE\Software\Classes and HKEY_CURRENT_USER\Software\Classes, with writes directed to HKLM. This is a shortcut to writing system default values, and many tutorials show this because it's slightly simpler, but unless you're installing the application for all users I don't recommend it.

Advanced registry info on MSDN

Factor Mystic
a bit confuse..what different between the main keys? which one should i put? current_user, class_root, local_machine? how to determine which one to use? thax in advance
A: 

Thank you very much for the responses. Very2 apreciate Them..

As Per Conlcusion, 3 ways on solving my prob. in easy understadable approach:

Adding shortcut in 3 ways:

1. create directly in registry window: http://www.codeguru.com/cpp/misc/misc/internetexplorer/article.php/c11007/

2. shortcut available only to folders. http://www.codeproject.com/KB/cs/appendmenu.aspx http://blog.voidnish.com/?p=17

3. shortcut available to all files and folders. http://www.autoitscript.com/forum/index.php?showtopic=103265&amp;view=findpost&amp;p=731920