Click here to Skip to main content
11,926,933 members (54,381 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked

Creating and Installing Internet Explorer Context Menu using C#

, 10 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Add an Internet Explorer context menu using an installer class


This article basically illustrates how to create an Internet Explorer context menu item upon installation using C#. Usually such projects are not standalone, and are included upon other features, but this will show a step by step guide to create one.


First, you need to understand how to manually add an Internet Explorer context menu item, and this is an interesting link to check.

Creating the Project

Your ingredients for such a project are:

  1. C# Class Library
  2. Setup Project

For the C# class library, delete the default class, right click on the project name and click Add New Item and choose installer class.

For the setup project, just add the primary output of the installer class project and add this output to all four default custom actions. (Right click on the setup project name, select View > Custom action.)

At the end, your solution should look like this:


Quite simplistic. Now we just need to adjust one more thing in the setup project before tackling the code. Basically we need to grab the target directory submitted by the user upon installation, to do this.

Select the Primary output added under the Install Action, and type in the CustomActionData as depicted in the picture below:


Creating the Installer Class

The main purpose of the installer is to create the registry keys, so here goes:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
//DON'T FORGET TO ADD THIS ONE - used for the Registry Class
using Microsoft.Win32;

namespace IEContextInstallerClass
    public partial class IEInstaller : Installer
        public IEInstaller()

        public override void Install(System.Collections.IDictionary stateSaver)

            //grab the target director and add to install.installstate
            //note that the grabbed parameters are exactly the same 
            //as the one we set above
            stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());

        public override void Commit(System.Collections.IDictionary savedState)
            //retrieve the saved state
            //adding items to the context menu of IE
            RegistryKey key;
            //1,2,4,8,10,20 in heximal are 1,2,4,8,16,32 in decimal (respectively)
            string keyValueInt = "16";
            //the location of our key
            string subKey = 
        @"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";
            //create it
            key = Registry.CurrentUser.CreateSubKey(subKey);
            //set the value
            key.SetValue("Contexts", Convert.ToInt32(keyValueInt), 
            //set the path to the action file
            key.SetValue(null, "file://" + savedState["TargetDir"].ToString() + 
            //and close

        protected override void OnBeforeUninstall
            (System.Collections.IDictionary savedState)
            //removing items from the context menu of IE
            string subKey = 
        @"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";

Now basically the installer class does the following:

  1. Upon installation, grabs the target directory and saves it so we can use it at a later stage
  2. Upon committing, creates the registry keys
  3. In case the user wishes to uninstall, simply deletes the keys

Creating the Action File

This action file is the actual code we are running when clicking on the context menu:

<SCRIPT LANGUAGE = "JavaScript"> 
// Get the window object where the context menu was opened.
var oWindow = window.external.menuArguments; 
// Get the document object exposed through oWindow.
varoDocument = oWindow.document;
 // Get the selection from oDocument.
// in oDocument.
varoSelect = oDocument.selection;
 // Create a TextRange from oSelect.
varo SelectRange = oSelect.createRange(); 
// Get the text of the selection.
var sNewText = oSelectRange.text; 
// If something is selected, alert showing the selected text.
if(sNewText.length != 0){

The above is a simple JavaScript that grabs the selected text and show it in a message box... you can have other actions depending on your needs. (The code above is based on this.)


Once you have completed all of the above, build your solution and install.

You should have the following key added to your registry:


If you open a new instance of Internet Explorer and right click on a selected text, you should be getting:



So basically you should now be able to create and delete registry keys upon installation with no problem at all.


Now this is definitely not something innovative or out of the ordinary, but I couldn't find something similar on The Code Project, so I thought of adding it.
Additionally, this is a compilation of items I found while "Googling" here and there...


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Riad MS Afyouni
Other Born Interactive
Saudi Arabia Saudi Arabia
Follow me:

You may also be interested in...

Comments and Discussions

QuestionGood job my friend Pin
sikkosh13-Jan-12 7:06
membersikkosh13-Jan-12 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 10 Feb 2008
Article Copyright 2008 by Riad MS Afyouni
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid