I wanted to know how can I add a new item to IE context menu (right click menu), so that the selected text from a website is copied, my winform application C# is opened and the text is pasted into a text box in my application.
1 Answers
You can add an entry to IE standard context menu to open your program. To do so, follow these steps:
Open registry and go to:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt
Create a new key, and set the name of key to the text you want displayed in the context menu as the name, for example:
Open My App
Right click on
(Default)
and chooseModify...
and set the value to path of an html file which will contains the command to open your application. For example:C:\OpenMyApp.html
Add a new
DWORD
value namedContext
and set it's value to hexadecimal11
or decimal17
. To see more options read documentation. Also in documentations said to add binary but I triedDWORD
instead and it worked. Also other extensions that I've seen useDWORD
.Use this content for your
C:\OpenMyApp.html
:<script type="text/javascript"> function getSelectionText(w) { var text = ""; if (w.getSelection) { text = w.getSelection().toString(); } else if (w.document.selection && w.document.selection.type != "Control") { text = w.document.selection.createRange().text; } return text; } var parentwin = external.menuArguments; var selection = getSelectionText(parentwin); var oShell = new ActiveXObject("Shell.Application"); var commandtoRun = "C:\\MyApp.exe"; oShell.ShellExecute(commandtoRun,"\""+selection+"\"","","open","1"); </script>
Then it's enough to copy your application to
C:\MyApp.exe
. Your application should handle command line arguments by acceptingstring[] args
as input parameters forMain
entry point or usingEnvironment.GetCommandLineArgs()
. Then it's enough to pass the argument to your form and show it on your text box.
For more information:

- 120,393
- 18
- 203
- 398
-
Worked! Thanks a lot. – Tacy Nathan Apr 07 '16 at 14:01
-
I'm getting the error: This file does not have a program associated with it for performing this action – Graham Apr 28 '16 at 15:03
-
@Graham The only file we are trying to run in above example, is 'C:\MyApp.exe' which will not cause such error. Check your file name and file extension and try again. – Reza Aghaei Apr 28 '16 at 21:28
-
My file is C:\test2\blah.exe and blah.exe just creates another file so that I can see that it has done something. When I run blah.exe on it's own it does as expected. – Graham Apr 29 '16 at 08:09
-
It's hard to guess what is the reason of such problem. If it can be [MCVE](http://stackoverflow.com/help/mcve), post a new question about it :) – Reza Aghaei Apr 29 '16 at 15:36
-
@RezaAghaei From above Java script, can you put the alert showing the text selected from IE? so everytime we select text from IE, and rightclick menu, alert pop up. – Raspi Surya Feb 21 '18 at 11:35
-
@RaspiSurya Above solution adds a new menu item to context menu, called *Open My App*. By clicking on the *Open My App*, it will run your application. Instead of running the application you can run any script which you need, including showing an alert. – Reza Aghaei Feb 21 '18 at 11:41
-
I have succeed adding the context menu, I just want to execute openmyapp.html from rightclick menu, and then show the selected text in alert. -------------- function getSelectionText(w) { var text = ""; if (w.getSelection) { text = w.getSelection().toString(); } else if (w.document.selection && w.document.selection.type != "Control") { text = w.document.selection.createRange().text; } return text; } alert(text); where did I miss? – Raspi Surya Feb 21 '18 at 11:43
-
In which line of the code showing the passing parameter from IE to the var text? – Raspi Surya Feb 21 '18 at 11:50
-
`var selection = getSelectionText(parentwin);` In this line of code, `selection` is the selected text. – Reza Aghaei Feb 21 '18 at 11:52
-
@RezaAghaei thank you very much! you are very good! it works! – Raspi Surya Feb 21 '18 at 11:59
-
@RezaAghaei one more thing Reza, How can I open my test.html site into the new tab / new browser via regedit? currently I just execute C:\qnavi.htm in REG_SZ. – Raspi Surya Feb 21 '18 at 14:06
-
@RaspiSurya I didn't tried, but if you create `var oShell = new ActiveXObject("Shell.Application");`, then `oShell.ShellExecute("http://www.google.com")` should open google in new tab/window of the browser. – Reza Aghaei Feb 21 '18 at 15:17
-
@RezaAghaei I try to use your method to get the html class in IE browser. So i put code : var title= document.getElementsByClassName("ttl"); var aNode = title[0]; alert(aNode); but it doesnt work. Do you know why? – Raspi Surya Feb 22 '18 at 09:34
-
@RaspiSurya Make sure you are using the correct document object. For my example, I should use document object of the passed window, for example: `w.document.getElementsByClassName('something')`. – Reza Aghaei Feb 22 '18 at 09:40
-
@RezaAghaei can you help to answer this https://stackoverflow.com/questions/49041265/get-external-website-url-using-external-javascript-in-ie-browser – Raspi Surya Mar 01 '18 at 06:33
-
@RaspiSurya Yes, I posted an answer for your question. – Reza Aghaei Mar 01 '18 at 07:47