How to add a context menu to the Windows Explorer in C#

 

This article demonstrate how to add a context menu in windows explorer for the your custom action. Suppose you can add a contextmenu to run your C# application executable.
The following code example create the some registry entries for adding context menu for the custom action:
 

       private void AddOption_ContextMenu()
       {
           RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Directory\\Background\\Shell", true);
           RegistryKey newkey = _key.CreateSubKey("Your Application");
           RegistryKey subNewkey = newkey.CreateSubKey("Command");
           subNewkey.SetValue("", "C:\\yourApplication.exe");
           subNewkey.Close();
           newkey.Close();
           _key.Close();
       }

 
Similarly you can remove the contextmenu by deleting created registry entry, see this:
 

       private void RemoveOption_ContextMenu()
       {
           RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Directory\\Background\\Shell\\", true);
           _key.DeleteSubKey("Your Application");
           _key.Close();
        }



And now if you want to add a new item on the context menu when you right click on a folder, you need to write registry entry in ‘Folder\Shell’ key, see the following:
 

       private void AddOption_ContextMenu()
       {
           RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Folder\\Shell", true);
           RegistryKey newkey = _key.CreateSubKey("Your Application");
           RegistryKey subNewkey = newkey.CreateSubKey("Command");
           subNewkey.SetValue("", "C:\\yourApplication.exe");
           subNewkey.Close();
           newkey.Close();
           _key.Close();
       }

 
Similarly you can remove the contextmenu by deleting created registry entry, see this:
 

       private void RemoveOption_ContextMenu()
       {
           RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Folder\\Shell\\", true);
           _key.DeleteSubKey("Your Application");
           _key.Close();
        }