如何执行已经启动的应用程序与文件关联的事件? [英] How to execute an event of already launched application with file association?

查看:168
本文介绍了如何执行已经启动的应用程序与文件关联的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个新的Windows窗体项目玩弄后,我发现,当你在Windows可执行关联的文件类型,你可以发现,使用的args [启动的应用程序文件的文件路径0] 静态无效的主要(字串[] args)

After playing around with a new Windows Form project, I discovered that when you associate a file type with an executable in Windows, you can find the file path of the file that launched the application using args[0] from static void Main(string[] args)

是否有可能发起的事件在你的应用程序,当你双击一个文件,如果你的应用程序已经打开? (由于明显主要(字串[] args)不会被触发)。

Is it possible to launch an event on your application when you double click a file if your application is already open? (As obviously Main(string[] args) won't be triggered).

与行为我试图复制应用程序的示例:

Example of an application with behavior I am attempting to replicate:

  1. 在用户打开GIMP(在Windows中)
  2. 在用户打开浏览器,右单击.png文件
  3. 在用户选择使用GIMP
  4. 开启
  5. 而不是创造GIMP的一个新的应用实例,GIMP打开图片在GIMP的一个已经打开的实例中的一个新窗口。

在这种情况下是GIMP使用多个应用程序接受文件关联打开文件?或者是有可能用一个单一的应用程序做实例。

In this case is GIMP employing multiple applications to accept files "opened" with file association? Or is it possible to do it with a single application "instance".

我有这个麻烦的,因为我的大部分搜索往往会导致我推向文件关联的Windows用户(即文章如何.xls文件与Excel关联)。

I'm having trouble with this as most of my searches tend to lead me towards file association as a Windows user (i.e. "How to associate .xls files with excel" articles).

推荐答案

雷蒙德是正确的,当然,但如果你正在寻找帮助的选项implmentation(1)你应该看看<一href="http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application">What是创建一个单实例应用程序的正确方法?和 .NET 4单个应用程序实例和<一href="http://stackoverflow.com/questions/7339920/switch-to-other-instance-of-same-application">Switch到同一应用程序的另一个实例

Raymond is right of course, but if you're looking for help with the implmentation of option (1) you should probably look at What is the correct way to create a single instance application? and .NET 4 single application instance and Switch to other instance of same application

您会发现,检测应用程序pretty的容易(使用互斥)。使其它应用程序,并在发送它的文件名可以是更具挑战性。

You'll notice that detecting the application is pretty easy (use a mutex). Bringing the other application and sending it a filename can be more challenging.

有psented的答案,previously连接问题的三个基本方案$ P $

There are three basic solutions presented in the answers to the previously linked questions

  1. 使用 PostMessage的 将消息发送给第一个实例。这将使用 HWND_BROADCAST 它可以有无人照管的后果。

  1. Use PostMessage to send a message to 1st instance. This uses HWND_BROADCAST which can have untended consequences.

使用<一个href="http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.applicationbase.aspx"相对=nofollow> Microsoft.VisualBasic.ApplicationServices.ApplicationBase 当然是一个参考的的VisualBasic 的给出了一些C#开发者心惊肉跳。

Use Microsoft.VisualBasic.ApplicationServices.ApplicationBase Of course a reference to VisualBasic gives some C# devs the willies.

使用 的FindWindow 依赖于Windows名称。

Use FindWindow which relies on a Windows Name.

其还值得一提的是,如果你想在现有的应用程序是在前面,你需要特别注意,因为设置前景色只能送人没有采取。请参见前景激活权限是一样的爱情:你不能偷它,它必须给你并的 AllowSetForegroundWindow SetForegroundWindow

Its also worth noting that if you want the existing application to be in the front you'll need to take special care because setting the foreground can only be given away not taken. See Foreground activation permission is like love: You can't steal it, it has to be given to you and AllowSetForegroundWindow and SetForegroundWindow

这篇关于如何执行已经启动的应用程序与文件关联的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆