我有一個MDI文件查看器。也就是說,我的程序可以在單個實例中打開多個文件。順便說一句,我的應用程序不是一個單一的實例應用程序,所以用戶可以根據需要打開任意多個實例。.NET:如何用現有實例打開文件?
我想要的行爲是
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.
我認爲這種行爲是很常見的。 Internet Explorer 9的工作原理類似於。所以,我相信以前肯定有很多人已經實施過這個。是否有任何完善的.NET(C#是首選)示例代碼(如果可能,不使用Win32 API)?我猜這個算法可能類似於下面的內容,但我不知道它是否是最好的或最乾淨的代碼來實現它(不使用Win32 API)。
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.
---- -----添加了對於 誰已經回答了我的問題與現有的回答那兩個人。
我的應用程序不是一個單一的應用程序!請。這就像Internet Explorer 9一樣。我在看WCF P2P,因爲我必須向我的應用程序的每個正在運行的實例廣播一個文件打開消息,然後從中選擇一個。但是使用WCF P2P似乎有很多工作要做,因爲它似乎是打開並監聽TCP端口。最佳做法是什麼?
對不起,我的應用程序是不是一個單獨的應用程序。我必須找到最後使用的實例並用它打開文件。 – 2011-06-10 08:13:07
哦!道歉,我會編輯答案。我錯過了那一點。我的藉口:在編程10個小時後離開辦公室之前。 :) – Maverik 2011-06-10 08:23:56