views:

115

answers:

2

Hi,

I would like to select several files on my desktop (or any other folder) and pass their names to my application.

More specifically, I've added a key to the registry such that when I right click on a jpeg file I see a new "Transform" option that actually runs my application. The question is how can I pass all selected files names to my application ?

Thanks a lot !

+9  A: 

They should be passed by windows. Look at the command line arguments passed to your application at start up. ie. in your Main function that has a parameter of string[] args.

To illustrate:

static void Main(string[] args)
{
    if (args.Length == 0)
    {
        Console.WriteLine("Program called without arguments");
    }
    else
    {
        Console.WriteLine("Program received this arguments:");
        foreach (string arg in args)
        {
            Console.WriteLine("\t{0}", arg);
        }
    }

    // .. do other stuff
}
TJMonk15
However, Windows will start a separate process for every selected file.
0xA3
So you say that the selected files will be passed to my application automatically by Windows ?Another problem that the Main() method that was created automatically does not have the "args" parameter: static void Main() {...}. It was created by Visual Studio when I started a new Windows Forms Application project.
@divo Great comment. So I believe he will need something that prevent multiple instances of his app from running, correct?
Josh Stodola
Yes, if *all* selected files should be passed to the same instance two things would be needed: A single-instance mechanism, and a mechanism to pass arguments from a newly started instance to the already running instance (e.g. as described here: http://www.codeproject.com/KB/threads/SingletonApp.aspx). Or, much simpler, use drag'n'drop to pass all file names at once.
0xA3
Guys, can you tell me why the Main() function does not have the "args" parameter ?
OK, I found that. Thanks.
A: 

If this is something that you want to do while your app is already running, like for example via a drag/drop type operation then there are a few API calls that allow you to perform drag and drop type operations with Shell objects.

Lookup/google IDragSourceHelper, IDropTargetHelper, IDataObject etc.

Tim Jarvis