Saturday, August 23, 2008

How to run applications under system control programatically through C#

This Article describes how to run applications under control of o/s programatically with C#.For example one have to open notepad programatically or Internet Explorer or any other *.EXE application installed in system.
.Net framework provides 'System.Diagnostics' namespace which provides types to interact with process under o/s.For our current requirement we need 'Process' class under 'System.Diagnostics' namespace.'Process' provides static & instance methods to run an *.EXE application.Below i presented code to open notepad programatically.

the above piece of code opens the notepad.
Its also possible to send parameters to Process with the help of 'ProcessStartInfo' class.
For example if one wants to open internetExplorer & navigate to Google home page programatically , it can be done with following piece of code.


ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "IEXPLORE";
startinfo.Arguments = "Http://";

When you run above code , a new IE window will be opened with Google home page.

How to Add Context Menu Item to Internet Explorer Browser.

This article describes about how to add custom context menu item to Internet Explorer Browser.This essentialy involves registry modifications.This can be done either manually or programatically.Manually means opening system registry & editing manually.Programatically in the sense writing code to edit registry.For programatic editing i'l present C# coding.
Context Menu:
Context menu is menu which will be opened on mouse right buttom click on a window.
Note:To understand this article better a basic understanding of system registry is essential.
Manul Way:
Internet Explorer has its own default context menu.If one wants to add custom menu item to this default menu,microsoft is providing a way to do this.This typically requires 'System registry' modifications.To add a context item open system registry.For this follow below navigation.

Start --> Run --->regedit
Then registry will be opened.In registry navigate to following path 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt'
Under 'MenuExt' key add a new key.The name of this key will be the name of menu item in Internet Explorer.For instance say 'MyMenuItem'.Once key is added a default string value is added to this key.The Value of the this default string should be the path of the Script file which will be executed when 'MyMenuItem' is clicked.For instance say 'C:\MyMenuItem.htm'This may be .js file or .htm file.Once this is done the menu item will appear in Internet Explorer Context Menu.
Now 'MyMenuItem' item will be appear in IE context menu.One can perform what ever the action needed by adding appropriate script in sript file.The window can be accessed from the script as follows.
//gets the window
var window=external.menuArguments ;
//to get the document object
var doc=window.document ;
//perform action .....

This is how a context menu item can be added to IE manually.If context menu item to work for specific items like Selected text,image or links, this can be done by specifying 'Context' value for 'MyMenuItem' key in registry.
To Add content specific context menu item to IE, Add 'DWORD' type value to 'MyMenuItem' key & name it 'Context'.The value of the 'Context' decides to which content the menu item is available.
The valid values of 'Context' & their corresponding contents are given below.
  1. 0x1 (Hex) -- this default
  2. 0x2 (hex)-- for images (means this context menu item available for only images )
  3. 0x4 " ---for controls
  4. 0x8 " --- for tables
  5. 0x10 ---for selected text
  6. 0x20 ---for Anchors(links)

Note:before editing Registry ensure that Registry back up is taken.

For More Info about Customising IE Check

Programatic Way:

.net framework provides built in API to work with system registry.

Note:Make sure that namespace 'Microsoft.Win32' is accessible to your code

RegistryKey registryKey;
registryKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt");

//Adding registry key 'MyMenuItem'
RegistryKey addSourceKey = registryKey.CreateSubKey("MyMenuItem");

//Set Default string value to Path of the script file
addSourceKey.SetValue("", "C:\\MyMenuItem.htm");