Introduction
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.
Background
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:
- C# Class Library
- 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;
using Microsoft.Win32;
namespace IEContextInstallerClass
{
[RunInstaller(true)]
public partial class IEInstaller : Installer
{
public IEInstaller()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());
}
public override void Commit(System.Collections.IDictionary savedState)
{
base.Commit(savedState);
RegistryKey key;
string keyValueInt = "16";
string subKey =
@"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";
key = Registry.CurrentUser.CreateSubKey(subKey);
key.SetValue("Contexts", Convert.ToInt32(keyValueInt),
RegistryValueKind.DWord);
key.SetValue(null, "file://" + savedState["TargetDir"].ToString() +
"\\action.html");
key.Close();
}
protected override void OnBeforeUninstall
(System.Collections.IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
string subKey =
@"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";
Registry.CurrentUser.DeleteSubKey(subKey);
}
}
}
Now basically the installer class does the following:
- Upon installation, grabs the target directory and saves it so we can use it at a later stage
- Upon committing, creates the registry keys
- 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">
var oWindow = window.external.menuArguments;
varoDocument = oWindow.document;
varoSelect = oDocument.selection;
varo SelectRange = oSelect.createRange();
var sNewText = oSelectRange.text;
if(sNewText.length != 0){
alert(sNewText);
}
</SCRIPT>
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.)
Installing
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:
Recap
So basically you should now be able to create and delete registry keys upon installation with no problem at all.
Note
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...