1

I develop a wpf app and i associated a file type named .fcsc. The application opens when the file is doubled clicked but it execute a new instance of the app. What i want is that if the app is already running, open the file in that instance not in a new one.

How can i archive that?

This is what i have when a file is open:

if (AppDomain.CurrentDomain.SetupInformation != null 
                && AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null 
                && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null &&
                AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Any())
            {
                // is a file association invoke, open the window
                InstallPluginWindow installPluginWindows = new InstallPluginWindow(AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData);
                installPluginWindows.Show();
                installPluginWindows.Owner = this;
                this.Opacity = 0.5;
                this.IsEnabled = false;
                installPluginWindows.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
                installPluginWindows.Closed += installPluginWindows_Closed;
            }
            else
            {
                NegotiateLogin();
            }
Misters
  • 1,337
  • 2
  • 16
  • 29

1 Answers1

1

The easy part is setting up a mutex to check for another instance. In your app main or startup code you waould need to so something like the code below:

    bool exclusive;
    System.Threading.Mutex appMutex = new System.Threading.Mutex(true, "MyAppName", out exclusive);
    if (!exclusive)
    {
      //Another instance running
    }    ...
    GC.KeepAlive(appMutex);

Next, you need to implement a way to message the first application instance and pass in the filename that was double clicked. You can do this in many ways, however, sending a custom message to the main window seems to be the most straightforward. Here is an alternative to message another application.

Community
  • 1
  • 1
Ross Bush
  • 14,648
  • 2
  • 32
  • 55