C# Windows 'Open With >' Context menu behaviour [duplicate]
The Open With command just passes the path of the file as the first argument to the application so all you need to do is
public static void Main(string[] args){ if(args[0] != null) { //args[0] contans a path to the file do whatever you need to do to display it } else { //Start normally }}
To automaticly put your program in the open with list you will need to add some reg keys in HKEY_CLASSES_ROOT\YOUR_EXT\
. Here is a SO answer saying how to do it
Or you could just add it by hand to the open with list the normal way.
Take a look at this blog post: Shell Extensions - Context Menu. It has code for a simple "wrapper" to some COM hooks to the Windows shell context menu. Put it in the GAC and when you right-click, your menu will be included as a sub-menu of the right-click context menu.
As far as strictly using "Open With..." to make your application show up ONLY for files it can open, that's a little easier. These are managed by Windows using registry keys in two places in the registry:
- HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ FileExts \ .FileExtension \ OpenWithList (install for current user)
- HKEY_CLASSES_ROOT \ .FileExtension \ OpenWithList (install for all users)
Take a look at some of the existing ones using regedit, then use the Registry class to create a new key for the extension you want.