How to add item to Windows shell context menu to open your application

Sometimes when installing applications, you want to add an entry to the windows shell context menu to open your application and pass the selected file or folder name to your application. In this post I’ll show how to that for context menus for Folders using a Visual Studio Setup Project, so after installing the application, when you right click on a folder, you can see such menu:

Shell Context Menu

To add an item to context menu of folder, the setup should do the following configurations in registry:

HKEY_CLASSES_ROOT
    Directory
        shell
            OpenWithMyApp  → (Default): Text of the menu
                             Icon:      "Path to executable" 
                command    → (Default): "Path to executable" "%V"

%V is the command line argument. The folder name which you right clicked on it will be passed to your application as command line argument.

Create the application

The application just needs some code to read command-line arguments. Adding the context menu will be done by setup project. Here is the code for application main menu:

[STAThread]
static void Main()
{
    string folderName = null ;
    if (Environment.GetCommandLineArgs().Length > 1)
        folderName = Environment.GetCommandLineArgs()[1];
    MessageBox.Show(folderName);

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(true);
    Application.Run(new Form1());
}

Create Setup Project to Add entries to Windows Resgitry

To add a setup project to the solution to install the service, in the New Project you can use Setup Project template. you can find it in the following hierarchy:

  • Other Project Types → Visual Studio Installer → Setup Project

If you don’t have Setup Project template, you should first download and install it for VS2017, VS2015 and VS2013.

After adding the setup project to the solution, you should add primary output of your main application to the setup project to install on target machine:

  • Right click on the Setup1 project in solution explorer → Add → Primary output …
  • In the window, select the project from combo box and Primary output from list box and press OK

Then you should add some keys to the registry. To do so:

  • Right click on Setup1 project → View → Registry

You can also click on registry toolbar button on toolbar of solution explorer:

setup-toolbar

Then in the setup registry editor window, add the following keys and values:

  • Right click on HKEY_CLASSES_ROOT → New Key → Set the name to Directory
  • Right click on Directory → New Key → Set the name to shell
  • Right click on shell → New Key → Set the name to OpenWithMyApp
  • Right click on OpenWithMyApp → New String Value → Delete the name and press Enter to change name to (Default)
  • Set the value to the text which you want to show in menu, for example Open with My App
  • Right click on OpenWithMyApp → New String Value → Set the name to Icon
  • Set the value to “[TARGETDIR]XXXXXX” which xxxxxx should be your application name, for example MySampleApp1.exe
  • Right click on OpenWithMyApp → New Key → Set the key to command
  • Right click on command → New String Value → Delete the name and press Enter to change name to (Default)
  • Set the value to “[TARGETDIR]XXXXXX” “%V” which xxxxxx should be your application name, for example MySampleApp1.exe

After that, the registry editor should be like these pictures:

open with my app

Note: When trying to set value for a registry value, you can select the registry value and set its Value property in Property Window.
Note: You can change the icon of application in Application tab of Application properties.

Then it’s enough to build all projects and run the generated setup.

Download

You can clone or download the working example:

You May Also Like

About the Author: Reza Aghaei

I’ve been a .NET developer since 2004. During these years, as a developer, technical lead and architect, I’ve helped organizations and development teams in design and development of different kind of applications including LOB applications, Web and Windows application frameworks and RAD tools. As a teacher and mentor, I’ve trained tens of developers in C#, ASP.NET MVC and Windows Forms. As an interviewer I’ve helped organizations to assess and hire tens of qualified developers. I really enjoy learning new things, problem solving, knowledge sharing and helping other developers. I'm usually active in .NET related tags in stackoverflow to answer community questions. I also share technical blog posts in my blog as well as sharing sample codes in GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *