本文介绍了在已经打开的程序中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个VB.NET程序,希望在系统托盘中运行该程序并处理从电子邮件中打开的附件.我创建了自己的文件类型,并将其注册到注册表中.一切工作正常,除了我不知道如何处理正在打开的程序中要打开的第二个文件.我知道如何检查是否有另一个实例正在运行,但是我不知道将控制权移交给已经打开的程序.有没有办法将打开定向到已经运行的程序?

谢谢

I have a VB.NET program that I have written that I want to run in the system tray and process attachments they are opened from e-mails. I created my own file type and registered it in the registry. Everything works good except I don''t know how on the second file that I''m processing to open it up in the already open program. I know how to check to see if there is another instance running but I don''t know to turn control over to the already open program. Is there a way to direct the open to the program that is already running?

Thanks

推荐答案

string mainModuleFile = System.Reflection.Assembly.GetEntryAssembly().Location;
System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();



现在,让我们考虑一下第一实例是否已经在运行.首先,使用System.Diagnostics.Process.GetProcesses迭代系统中的所有进程.对于迭代循环中的每个进程,请使用Process.MainModule实例属性找到其主模块.此属性返回类System.Diagnostics.ProcessModule的实例,而System.Diagnostics.ProcessModule.FileName为您提供全路径文件名,以与您当前正在运行的进程之一进行比较.如果您有匹配项,则运行此代码的实例不是第一个.您可以采取必要的操作并退出应用程序.

原则上,可以修改此方法以使用稍微不同的条件进行匹配,使用附加的Process属性,但不需要完全匹配路径名; ProcessNameModulesMainWindowTitle(如果适用),MainModule.ProcessName等属性;以这种方式,即使其可执行文件不同,也可以检测到相同"应用程序的先前实例.同样,过程属性的任何组合都不能保证这是相同"的应用程序.

这还不是全部...

待续...



—SA



Now, let''s take care about first instance if it is already running. First, iterate all processes in the system using System.Diagnostics.Process.GetProcesses. For each process in the iteration loop, find its main module using Process.MainModule instance property. This property returns the instance of the class System.Diagnostics.ProcessModule, and System.Diagnostics.ProcessModule.FileName give you the full-path file name to compare with the one of your process you''re currently running. If you have a match, the instance running this code is not the first. You can take required actions and exit the application.

In principle, this method can be modified to use a little bit different criterion for the match, using additional Process properties but not requiring exact match of the full path name; the properties like ProcessName, Modules, MainWindowTitle (if applicable), MainModule.ProcessName, etc.; in this way, a previous instance of the "same" application can be detected even if its executable file(s) is different. Again, no combination of the process properties can guarantee this is the "same" application.

This is not all…

To be continued…



—SA




这篇关于在已经打开的程序中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:56
查看更多