Opening a “known file type” into running instance of custom app - .NET

前端 未结 5 1693
情歌与酒
情歌与酒 2020-11-30 06:56

How would you open a file (that has a known file/app association in the registry) into a \"running instance\" of the application it\'s supposed to open in? An example would

相关标签:
5条回答
  • 2020-11-30 07:35

    What you want to do is inherit a class from WindowsFormsApplicationBase, setting the protected IsSingleInstance property to true:

    // This should all be refactored to make it less tightly-coupled, obviously.
    class MyWindowsApplicationBase : WindowsFormsApplicationBase
    {
      internal MyWindowsApplicationBase() : base()
      {
        // This is a single instance application.
        this.IsSingleInstance = true;
    
        // Set to the instance of your form to run.
        this.MainForm = new MyForm();
      }
    }
    

    The Main method of your app then looks like this:

    // This should all be refactored to make it less tightly-coupled, obviously.
    public static void Main(string args[])
    {
      // Process the args.
      <process args here>
    
      // Create the application base.
      MyWindowsApplicationBase appBase = new MyWindowsApplicationBase();
    
      // <1> Set the StartupNextInstance event handler.
      appBase.StartupNextInstance = <event handler code>;
    
      // Show the main form of the app.
      appBase.Run(args);
    }
    

    Note the section marked <1>. You set this up with an event handler for the StartupNextInstanceEvent. This event is fired when the next instance of your app is fired when you have a single instance application (which you specified in the constructor of MyWindowsApplicationBase). The event handler will pass an EventArgs-derived class which will have the command line arguments which you can then process in the running instance of your app.

    Then, all you have to do is set the file associations normally for the file types you want your app to process, and you are set.

    0 讨论(0)
  • 2020-11-30 07:35

    The way I'd do it is like this:

    1. First thing in the main method, check the process list for an existing instance of the application.
    2. If found, send the filename/path to the already running instance using your favorite interprocess communication method (sending windows messages, remoting, wcf, etc.)
    3. Close the new process that windows tried to start (since the existing instance already handled the file open operation
    0 讨论(0)
  • 2020-11-30 07:36

    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"

    0 讨论(0)
  • 2020-11-30 07:37

    Looks like what you are looking for is creating a single instance application. This can be done in C# by using WindowsFormsApplicationBase located in Microsoft.VisualBasic.dll

    For details, take a look at: http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx or search for WindowsFormsApplicationBase

    0 讨论(0)
  • 2020-11-30 07:49

    Windows uses DDE for this purpose.

    Dynamic Data Exchange (DDE) is a technology for communication between multiple applications under Microsoft Windows or OS/2.

    Registry associations for word or office files usually have DDE commands in addition to the usual file association (to be executed if the app already is running).

    So you can host a DDE server in your C# app to implement this functionality.

    0 讨论(0)
提交回复
热议问题