Dave_P
Dave_P

Reputation: 173

C# Environment.GetCommandLineArgs() from context menu

I posted another question earlier about getting the arguments from a context menu App. If you select an item it gives you the string path to that item, but it starts a new instance of the app for every item selected greater than one. With Environment.GetCommandLineArgs() it gives you a string array with the first element is the .exe calling the function and the second element being the string path of the item selected. Again, If I select 2 or more items and right-click >> run app, I get 2 or more instances of the app, each with the first element as the .exe followed by the second element as one of the items selected. (I did this with MessageBox.Show() after joining the two elements and it pops up the message box 3 times, 1 for each of the three items selected).

Now I am using Mutex to only allow it to run once but I only get the first message box (as expected).

How can I get all the items listed in one instance if I select more than one Item?

Here is the code without mutex:

static void Main()
    {
        String[] args = Environment.GetCommandLineArgs();
        var message = string.Join(", ", args);
        MessageBox.Show(message);
    }

And here it is with Mutex:

static void Main()
    {
        Mutex startOnlyOne = new Mutex(false, "WinSyncSingalInstanceMutx");
        if (startOnlyOne.WaitOne(0, false))
        {
            String[] args = Environment.GetCommandLineArgs();
            var message = string.Join(", ", args);
            MessageBox.Show(message);
            startOnlyOne.Close();
        }

No one has been able to help me out with this yet, I hope someone can help me figure this out. Thanks in advance...

Upvotes: 0

Views: 1535

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40150

You need an external process to handle this, with your shell extension merely being the "trigger" mechanism. Don't try to keep the shell extension itself as a single instance app.

Instead, you can create a service which listens for incoming events from your extension, perhaps via WCF. Then it can do whatever you need with the incoming file paths.

Upvotes: 1

Related Questions