开发者

.NET: How to open a file with an existing instance?

开发者 https://www.devze.com 2023-03-11 23:07 出处:网络
I have an MDI file viewer. That is, my program can open multiple files in a single instance. By the way, my application is not a single instance application, so t开发者_Go百科he users can open as many

I have an MDI file viewer. That is, my program can open multiple files in a single instance. By the way, my application is not a single instance application, so t开发者_Go百科he users can open as many instances as they want.

The behaviour I want is

When the user double-clicks a file in Windows Explorer,
if there is an existing instance of my application,
   then open the file with that instance
else
   open the file with a new instance.

I think this behaviour is very common. Internet Explorer 9 works like. So, I believe that there must have been many people who have already implemented this before. Is there any well-established .NET (C# is preferred) sample code for this (not using Win32 API's, if possible)?

I guess the algorithm could be something like the following, but I don't know if it is the best or the cleanest code to implement it (not using Win32 API's).

At the program's start up
    1)If there are arguments in Main(),
         check for existing instances.
    2)If an instance exists,
         send a message to the instance so that it can open the file.
         Then exit.
    3)else
         open the file.

----Added----- For those two persons who have answered to my questions with existing answers.

MY APPLICATION IS NOT A SINGLETON APPLICATION! Please. It's just like Internet Explorer 9. I was looking at WCF P2P, since I have to broadcast a file open message to every running instance of my application, then choose one among them. But using WCF P2P seems to be a lot of works for this, because it seems to be opening and listening to TCP ports. What would be the best practice?


I found another one very similar:

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


Edit: Since your application isn't singleton, check the link that Doc Brown pointed out (Opening a "known file type" into running instance of custom app - .NET), there is a relevant answer in that link by Joel Martinez.

I'll quote his reply for quick reference:


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

I'm guessing you already know how to do IPC considering you've been through WCF P2P, but if not, I'll drop a link for that too. Although the answer is targeted at singleton scenario, it will work perfectly for you too. Just pick the last instance by creation datetime or whatever criteria suits your scenario.

Do note however that you can't pick up the last active instance through this method without actually keeping track of that somehow yourself and polling that information from your fresh instance.

Reference: System.Diagnostics.Process.GetProcesss

0

精彩评论

暂无评论...
验证码 换一张
取 消