Get multiple file paths with a right click

I added my application to the Windows context menu using the registry

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

I can get the path to the selected file in MessageBox using the code below.

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

This is fine if I want to open one file, but if I select several files, it launches several instances of my application. I need the path of the entire selected file in only one instance. Can someone give me an idea on how to do this?

+8
c #
source share
5 answers

Unprogrammed work to copy all paths at once: (tested on Windows 7):

  • Select the files you want.
  • Shift + right click.
  • Click "Copy as path" (it appears as an additional option!).

All paths are copied as expected!

+1
source share

I tried this and the application starts a new instance for each selected file. I even tried adding "% 2", "% 3", etc. To the registry team, which also did not work.

If you really need to run one instance for all files, perhaps you can configure the application as with a class derived from WindowsFormsApplicationBase , set the IsSingleInstance property and override OnStartupNextInstance (there is no link to this because my reputation does not allow more than two links)

+1
source share

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 several file paths using the Right Click shortcut that you did with the registry. However, there are some workarounds for this approach that may be useful;

  • If you really need it simply, you will have to deal with application instances using the Mutex class . This can help you if you manage to limit the instance of your application to one using Mutex. For this method to work for you, you need to pass command line arguments from all previous instances to the last instance.

The method I explained now includes a lot of code to get all the file paths at startup, however the second method is easy for this situation, but it is a little more complicated and requires something more than your executable file.

  • The second (recommended) approach, as I said, is a little more complicated, since it involves creating an additional component with your application to get all the paths to the file, but it ensures that you get all the paths at once (without multiple instances). The second approach may be called "Shell Extensions." To explain this approach, let me clarify some of the premises (and feel free to skip them if you already know about them). Shell extensions COM servers that are registered on a user computer with a specific type of file that they can process, say .cs, .csproj (both registrars must be opened with Visual Studio by default). Shell extensions are also called context menu handlers . A registered shell extension or context menu handler is invoked as soon as the file extension registered to open it receives a right-click from the Windows Explorer context menu.
    Generally speaking, a shell extension or contextmenu handler is a registered COM server that comes in the form of a Dynamic Link Library on the user's computer and its functions are called if the registered file type gets a right-click or double-click.
    So, I think you have enough to get started with Shell Extensions. Here 's a step-by-step article to provide you halfway with a decent starting point.

I think this will help you achieve your needs. If something remains, let me know.

0
source share

Although this is much more complicated, I know that a shell extension handler will solve this problem.
How to write a Windows Shell extension with .NET languages

I wrote one of these for a Windows form application that opens when users right-click on a specific file type. It also works for multiple files.

Good luck

0
source share

Your application, most likely, does not start several instances of itself, you just get several message boxes, since you said that the foreach loop shows a message box with each path. You need to manipulate the string to contain all the paths and then insert them into the message box.

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); } 
0
source share

All Articles