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.

6 Comments

  1. I got this error message when click command on folder context menu.
    Error – “This file does not have an app associated with it for performing this action. Please install an app or, if one is already installed, create an association in the Default Apps Settings page”

    Please tell me how to fix this.

  2. Hi Reza,

    You used context menu here for folder but if we want to use for all files of specific file types like (pdf, word, jpeg etc) then which registry setting should we target. please help me in this regards.

  3. Nice, I kinda forgot how to do this.
    Some people saying copying a shortcut of the executable to your shell:sendto location (normally C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\SendTo) will also work.

  4. Hello,
    This information was very helpful for me, so thanks… but now I´m facing a problem when selecting multiple files, it is opening individual instances for each file selected, and I don´t want this because I need to manage cross information that is inside all the files. How can I do this kepping one instance ? I´m passing filepath of each selected file.

  5. Hello sir,

    I searched ur answer about paint winforms (move shapes ). i need ur help .

    I also need to create the paint like application which should draw basic shapes like rectangle , circle ,line ,arrow ,and draw transparent text box , undo , redo, save, save as,

    i can draw shapes but i can’t move them…. i m not so good in c#.

    can u please help….

    that will be your most kindness …

Leave a Reply

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