A simple C# function to add context menu items in Explorer

I am currently working on a simple C# Windows Forms application to help me with editing/authoring articles for CodeProject, that basically strips unwanted files off zip files, and I wanted to associate an Explorer context menu entry with zip files that’d open the zip in my application. Of course, this was a mere matter of editing the registry and adding an odd key there and an odd value here. But I thought the function would be pretty useful for other people who might want similar functionality and so wrote it generically.


Here is how you can use the function :-

AddContextMenuItem(".zip", "ZipStrip",
  "Open with &ZipStrip", Application.ExecutablePath + " %1");

Code Listing

I had to mess with the indentation to avoid horizontal scrolling ๐Ÿ™‚

//Extension - Extension of the file (.zip, .txt etc.)
//MenuName - Name for the menu item (Play, Open etc.)
//MenuDescription - The actual text that will be shown
//MenuCommand - Path to executable
private bool AddContextMenuItem(string Extension,
  string MenuName, string MenuDescription, string MenuCommand)
  bool ret = false;
  RegistryKey rkey =
  if(rkey != null)
    string extstring = rkey.GetValue("").ToString();
    if( extstring != null )
      if( extstring.Length > 0 )
        rkey = Registry.ClassesRoot.OpenSubKey(
        if(rkey != null)
          string strkey = "shell\\" + MenuName + "\\command";
          RegistryKey subky = rkey.CreateSubKey(strkey);
          if(subky != null)
            subky = rkey.OpenSubKey("shell\\" +
              MenuName, true);
            if(subky != null)
            ret = true;
  return ret;

33 thoughts on “A simple C# function to add context menu items in Explorer

  1. Nish, I’m sure you are psychic. I was just planning out my next app when I decided to add explorer menu items. I hate registry stuff as finding anything in it is a nightmare. You just saved me at least an hours programming. Cheers.

  2. Glad to be of help, Nonny ๐Ÿ™‚

    BTW be sure to check the return value (it will be true on success), because if you don’t have write-rights to HKCR, the function will fail.


  3. hi nish,
    Iam a aptech student in from fiji and currently facing some problems with c#, could you please e-mail me some source codes explaining storing values and manupulating it using stuctures.
    Thank You

  4. Nice code, congratulations!

    If I place “.bmp” in extension and I select 20 bmp files in explorer and execute the command, the explorer will open 20 instances of my program. How I do to these 20 bmp files will be, instead, 20 args of the command line???


  5. This looks great. One question I have is how would I go about getting file names with spaces? (I’ve tried putting quotes around the %1). What it is doing is actually passing the old DOS filename (c:\blah\blahh~\blah~.ZIP) to my program. If I use the FileInfo object and do a MessageBox.Show(fi.Name) it shows the DOS filename convention. My program actually parses the file name, so I would need some way to get that from this.


  6. This helped me a lot. Just one doubt. How do I enable/disable a context menu. I want my context menu to appear only if something is copied to the clipboard. By something I mean Folder(s).

  7. Instead of messing with the indentation, you might have replaced “if(!null) {}” with “if(null) return false;”

    Just 2 cents ๐Ÿ˜‰

  8. That was a good article nish. I need further enhancement in my project that is I must add icon to the menu item. How can I do that.

    Thanks in advance.

  9. what if i wanna have 2 or more different program with the same MenuName? eg, *.doc, *.txt, *.jpg send into the function..?

    thank u very much

  10. GShaik and Nish, would you please show how an icon(transparent) can be added at the left side of the added text like WinRAR

  11. If my program is started by the context menu – as far as I can tell no commandline argument is sent to the app!! So how can I make use of this?!

    Thanks for the post anyway!

  12. Thanks Nish, It’s exaclty what I needed.
    bookmaked you’re blog… lots of interesting stuff

    btw… another method for removing Explorer context menu could be helpful too ๐Ÿ™‚

  13. Thanks for the simple but very useful code!

    A tip: surround the MenuCommand with quotation marks to allow the application to open files containing spaces — in your example it should be like this:

    “\”” +Application.ExecutablePath + “\” “\%1\””

  14. thanks for the tutorial. but when I click on my option in the MENU, I receive an error. how can I do to Launch my application after the click?

  15. a great tutorial. however, in function main (string [] args) is still missing save path parameter. I do not know how to do this? My goal is to add my program into ContextMenu (right click on excel file when my program and select the program will process excel file)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s