8

I have added my application to the right click menu of Windows with the help of the registry

"C://myapp.exe "%1"

I am able to get the path of the selected file in a MessageBox using the below code.

static void Main(string[] args)
{
   foreach (string path in args)
   {
       MessageBox.Show(path);  
   }
}

It is okay if I want to open a single file, but if I select multiple files, it runs multiple instances of my application. I need the path of all selected file in the single instance only. Can anyone give me an idea of how to do this?

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Krish
  • 81
  • 1
  • 2
  • Whats the problem? you need multiple arguments passed in? – Sayse Aug 21 '13 at 06:29
  • There's no way I'm aware of to distinguish "multiple selection, single right click, launch" from "select, right click, launch, select another, right click, launch" - so if you're happy, in both cases, for only a single instance to be running, you're looking to implement a "single instance application" - try searching for those three words plus c#. – Damien_The_Unbeliever Aug 21 '13 at 06:30
  • 1
    possible duplicate of [Open all files when user right clicks and selects "Open With"](http://stackoverflow.com/questions/3158312/open-all-files-when-user-right-clicks-and-selects-open-with) – Royi Mindel Aug 21 '13 at 06:43
  • 1
    Having single instance of my application is not a big issue, but passing more arguments to that single instance is the problem. I want to pass more arguments to that instance by selecting more files simultaneously. – Krish Aug 21 '13 at 07:33
  • 1
    You can tak this as exmple, what I am actually trying to do. I right click on a text file and open it with my executable file. I can get the path of that text file in the text box. But now there are more text files and I want to grab the path of all those files in the same textbox by one click. – Krish Aug 21 '13 at 07:40
  • Would all paths that a users selects not be in the same directory. If yes, why not always only show the path to the first entry in args only? – Rob Aug 29 '13 at 11:21
  • This seems to be a duplicate. Here is a similar question which is already answered: http://stackoverflow.com/questions/1826791/how-to-pass-in-multiple-file-folder-paths-via-a-rigth-click-eventverb-to-an-ex – mathsRuinedme Sep 09 '13 at 18:02

5 Answers5

1

A non-programming work-around, to copy all the paths simultaneously is the following (tested at Windows 7):

  1. Select the files you want.
  2. Shift+Right Click.
  3. Click "Copy As Path" (it appears as an additional option!).

All paths are copied as expected!

1

I tried it and the application is running a new instance for each selected file. I even tried to add "%2" "%3" etc to the registry command, that also did not work.

If it is really necessary to run a single instance for all files, maybe you can setup your app as with a class derived from WindowsFormsApplicationBase, set the IsSingleInstance property and override OnStartupNextInstance (no link to that 'cause my reputation does not allow more than 2 links)

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Here is good example for a [single instance app in c#](http://www.codeproject.com/Articles/12890/Single-Instance-C-Application-for-NET) – René Vogt Nov 06 '15 at 13:43
  • Yes, this is the way to go. There is no point in detecting new application instances and handle the communication between those yourself. Note: You will need a reference to *`Microsoft.VisualBasic.dll`* and a `using Microsoft.VisualBasic.ApplicationServices;` – Olivier Jacot-Descombes Nov 06 '15 at 16:06
0

First of all the most important thing that I would like to tell you about your approach is that it is absolutely impossible to get multiple file paths with the Right Click shortcut you have made with the registry.However there are some workarounds on this approach that might be helpful;

  • If you really need this simple then you'll have to deal with application instances using Mutex Class.This may provide you some help if you managed to limit your application instance to single using Mutex.To get this method to work for you,you'll have to pass commandline arguments from all the previous instances to the last instance.

The method I explained just now involves a lot of code to get all file paths during startup,however the second method easy is best suited this situation,but it's a bit difficult and requires something more than your executable.

  • The second (recommended) approach as i said is a little bit tricky as it involves creating an additional component with your application to get all the file paths,but it ensures that you recieve all paths at one go (no multiple instances).The second approach may be titled as Shell Extensions. To explain this approach let me explain some prerequisites (and don't hesitate to skip them if you already know about them).Shell Extensions are COM Servers,which are registered on the user's computer with some specific type of file they can handle say .cs , .csproj (both are registered to be opened with Visual Studio by default).Shell Extensions are also called Context Menu Handlers .A registered shell extension or contextmenu handler gets called as soon as the file extension registered to be opened with it receives a right click from Windows Explorer's contextmenu.
    Broadly speaking a shell extension or a contextmenu handler is a registered COM Server which exits in the form of a Dynamic Link Library in the user's computer and its functions are called if a registered file type receives a right click selection or double click.
    So with that I guess you have got enough to make a good start towards Shell Extensions.Here is a nice step-by-step article to provide you halfway with a decent starting point.

I guess that will help you achieve your need.If there's anything left,please let me know.

devavx
  • 1,035
  • 9
  • 22
0

While it is considerably more complicated I know that a Shell Extension Handler would solve this problem.
How to Write Windows Shell Extension with .NET Languages

I wrote one of these for a windows form application that opens when the users right clicks a specific file type. It works for multiple files as well.

Good luck!

John Yost
  • 693
  • 5
  • 20
0

Your application is most likely not running multiple instances of itself, you are simply getting multiple messageboxes since you told the foreach loop to show a message box with each path. You need to manipulate a string to contain all the paths and then insert them into the messagebox to show.

FROM

static void Main(string[] args)
{
   foreach (string path in args)
   {
      MessageBox.Show(path);  
   }
}

TO

static void Main(string[] args)
{
  string paths = "";
  foreach (string path in args)
  {
     paths += path + Environment.NewLine;
  }
  MessageBox.Show(path);
}