Alexei
Alexei

Reputation: 127

How to open associated files in the same instance of an application

I want to open files, which are doubleclicked in Explorer, in the same instance of my .Net application, e.g. in a new tab. How can I do this?

For now each file starts new instance of my application. Many programs can open files in the same instance, for example, Opera and Notepad++, so there is an easy way for sure.

Upvotes: 9

Views: 2485

Answers (4)

user3582780
user3582780

Reputation: 43

Example using TCP-sockets: http://pieterjan.pro/?a=Projecten_csharp_DrawIt.php

  1. start TCPListener on the form
  2. connect TCPClient in the main of the second instance
  3. Send ActivationArguments through the TCP-connection to the form

Works for multiple files at once as well, and even for multiple files at the first time (when application not started yet)

The most important code blocks are:

  1. The constructor of the MainForm (Hoofdscherm) where the server is started and the port number is written to a file. The first file is opened as well.
  2. The Main-function (Program.cs) where the second, third, ... instance connects to the TcpListener in the first instance and sends the filename through the socket

The source code is available on the button "Broncode"

Upvotes: 0

Benlitz
Benlitz

Reputation: 2002

In case you want to do the same, but in WPF rather than WinForms, the howto is explained here: What is the correct way to create a single-instance application?

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

You may take a look at this post which illustrates a technique that could be used to have a single instance WinForms application.

Upvotes: 4

Hans Olsson
Hans Olsson

Reputation: 55009

Might be an easier way to do it but the way I've done it is that if an instance is started with a filename as a parameter then it checks if there are any other instances and if so passes on the filename to that instance and the close itself down.

Upvotes: 1

Related Questions