views:

73

answers:

2

Hi,

I would like to write a C# .NET program that will do some transformations to selected images. After I select several images on my desktop (or any other windows folder) and right click them, I would like to see my program in the appeared menu. How can I insert my program to this menu, and execute it when this option is clicked ? I would appreciate a little code example.

Thanks in advance !

+1  A: 

check out this question and this one as well

Alexander Taran
I don't believe this addresses the intent of the question - it's not "how do I configure 'open with' for certain file extensions", but rather "how do I run custom actions". For example, how do I write a "update this folder from the source repo" command.
dpurrington
open is an action with name "open". any action can be called open, or open can be called anything. Anyhow, the method to do "Custom Action" is the same, only you name it "Custom Action", not "open".
Alexander Taran
+3  A: 

The easiest way to do this is not through code but through registry entries. Add a key under HKCR / filetype / Shell (where filetype is the filetype associated with the file extension, e.g. jpegfile). The name of the key is your menu option (e.g. "Transform"). Under this create a key named Command, and set the value of this key to the command line for your program e.g. c:\myprogram\myprogram.exe "%1" (the %1 is where Explorer will substitute the file name of the right-clicked file).

itowlson
Actually, I want the "Transform" option to be added automatically to the menu each time I run the program (and I will run the program on startup). I don't want to enter to the registry and edit it every time I put my program in another computer.
I'd still go with the registry approach: on program startup, check for the presence of the key, and insert it if it's not already there. You can use the Microsoft.Win32.RegistryXxx classes to do this. I don't know if there is an API to "temporarily" add entries to the context menu, or to get a callback (in a separate process) to intercept/modify when the context menu is being displayed (I believe you can do this from a shell extension, but then you need to get your extension loaded into Explorer, which is usually a registry setting anyway!). Sorry.
itowlson
OK, I understand how to add a key to the registry. But, I still can't figure out the flow of the program. Say I added my program to the startup programs of Windows. When Windows starts my program will start and will add a key to the registry. My first question is what should be the value of the Command key ? Should it be another program that transforms the image ? Or it can be the same my program somehow ? My second question is: when several files are selected, how do they names are passed to the Transform program ? Thanks a lot for your time !
The value of the Command key should be the command line you want to run when the user chooses the Transform menu item. This can certainly be the same as your program (not forgetting the %1 placeholder though!). This will start a new program instance: if you don't want that, you can make the new program instance hand over to your existing instance using the normal single-instancing techniques. I am not sure of the behaviour for multiple files: I think the program specified in Command is run for each of them (separately), but I'd advise doing a quick test to make sure.
itowlson
Well, I guess the easiest would be to run another program that makes the transformation. Thanks !