How to execute an event of an already running application with file association?

After playing with the new Windows Form project, I found that when you associate a file type with an executable file in Windows, you can find the file path of the file that launched the application using args[0] from static void Main(string[] args)

Is it possible to fire an event in an application by double-clicking a file if your application is already open? (As obvious, Main(string[] args) will not run).

Example application with the behavior I'm trying to replicate:

  • User opens GIMP (on Windows)
  • User opens explorer and right-clicks .png file
  • User selects open using GIMP
  • Instead of creating a new instance of the GIMP application, GIMP opens the image in a new window in the GIMP instance that has already been opened.

In this case, does GIMP use several applications to receive files β€œopen” with file associations? Or it can be done with a single application instance.

I am having problems with this, since most of my search queries tend to lead me to associate files as a Windows user (ie, β€œHow to link .xls files to excel”).

+2
source share
4 answers

Raymond, of course, is right, but if you are looking for help in implementing option (1), you should probably see What is the correct way to create one instance of the application? and .NET one instance of the application and move to another instance of the same application

You will notice that detecting the application is quite simple (use a mutex). Attracting another application and sending its file name can be a more difficult task.

In answers to previously asked questions, three main solutions are presented.

It is also worth noting that if you want the existing application to be in the foreground, you will need to take special care, because the installation of the foreground can be discarded unchecked. See Enable foreground activation is like love: you cannot steal it, it must be provided to you and AllowSetForegroundWindow and SetForegroundWindow

+2
source

There are many options, but none of them are free.

  • Your Main() program can detect that there is another copy already running and pass the file name to the already running copy in some ways that you define.
  • Your program can register as a DDE server and request a subsequent opening through DDE. This is an old-fashioned method since 1990, which is usually not recommended for new programs.
  • You can register a custom target for your application and transfer the target to transfer the file name to an already running copy. This mechanism takes the form of shell expansion and therefore is not suitable for C # due to problems with CLR injections.

Given your limitations, option (1) seems to be the best option.

+4
source

Microsoft created this feature for Visual Basic.Net, and it can also be used in C #.

See the following post for a simple example in C #:

http://www.openwinforms.com/single_instance_application.html

0
source

This approach worked best for me: Accepting applications with a single instance of WPF . In particular, its solution for passing arguments also works in applications for icon notifications only.

0
source

All Articles