相关问题[stackoverflow]此处。
我正在尝试执行上述操作,但我想更进一步。我想使用文件类型的默认编辑器打开任意文件。从那时起,我希望允许我的用户像平常一样与文件交互,或者继续在我的应用程序中工作。扩展是用户完成编辑后发生的事情。有没有办法可以从外部应用程序捕获关闭(最好是保存)事件并将其用作触发器来执行其他操作?出于我的目的,跟踪外部应用程序的关闭就可以了。
我可以根据具体情况执行此操作。例如,我可以从我的应用程序打开一个 Word 实例并跟踪我的应用程序感兴趣的事件。但是,我希望将我的应用程序与 Word 分离。我希望允许我的用户使用他们选择的任何文档编辑器,然后在幕后单独管理正在编辑的文件的存储。
Related question [stackoverflow] here.
I'm trying to do the above, but I want to take the process one step further. I want to open an arbitrary file using the default editor for the file type. From that point, I want to allow my user to interact with the file as they would normally, or continue to work in my application. The extension is what happens after the user finishes editing. Is there a way I can capture a close (and ideally save) event from the external application and use that as a trigger to do something else? For my purposes, tracking the closing of the external application would do.
I can do this in the specific case. For example, I can open a Word instance from my application and track the events that interest my application. However, I want to de-couple my application from Word.I want to allow my users to use any document editor of their choice, then manage the storage of the file being edited discretely behind the scenes.
发布评论
评论(4)
您可以按照与引用的问题类似的方式执行此操作,但语法略有不同:
WaitForExit() 调用将阻塞,直到其他应用程序关闭。您可以在单独的线程中使用此代码,以便用户可以同时继续使用您的应用程序。
You can do this in a manner similar to the referenced question, but the syntax is slightly different:
The WaitForExit() call will block until the other application is closed. You would use this code in a separate thread so that the user can keep using your application in the meantime.
使用
FileSystemWatcher
类 监视文件的更改。编辑:您还可以处理
退出
事件noreferrer">Process
对象来查找程序何时退出。但是,请注意,这不会告诉您用户关闭了您的文件,但也没有退出该进程。 (这在 Word 中尤其可能)。Use the
FileSystemWatcher
class to watch for changes to the file.EDIT: You can also handle the
Exited
event of theProcess
object to find out when the program is exited. However, note that that won't tell you of the user closes your file but doesn't exit the process. (Which is especially likely in Word).要监听文件更改,您可以使用 FileSystemWatcher 并监听最后修改日期的变化。
您还可以监视进程并在进程关闭时检查并归档。
To listen for file change, you can use the FileSystemWatcher and listen for a change in the last modified date.
You can also monitor the process and check then file when the process close.
我刚刚在网上发现了这个有用的提示。这似乎就是您正在寻找的东西。 本文(链接已损坏) 有一些关于 C# 编程的更详细、有用、切中要害的技巧。
I found this useful tip online just now. It seems to be what you are looking for. This article (link broken) has some more detail and useful, to-the-point tips on C# programming.