我有一个 MDI 文件查看器。也就是说,我的程序可以在一个实例中打开多个文件。顺便说一下,我的应用程序不是单实例应用程序,因此用户可以根据需要打开任意数量的实例。
我想要的行为是
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 端口。最好的做法是什么?
最佳答案
关于.NET:如何使用现有实例打开文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6275944/