Filesystem Hook for .doc open C#

陌路散爱 提交于 2020-01-14 03:20:10

问题


I'm writing an application that needs to be notified of every doc file that is opened, I've tried using the FileSystemWatcher but it seems that these days NotifyFilter.LastAccess is disabled due to a large overhead.

There is LastWrite which I suppose I could use but it would mean I'd need to try and figure out the original file name from the temporary file that word creates when it opens a document.

I also need to keep watch on 4 directories so ideally I don't want to be polling them.

I'm aware I could write a WordAddin which is one option but that means another deployment to manage, another codebase and another product to support along with the problem that many users always see addins as a source of slowdowns.

Is there a straightforward way to tell windows Vista upward that I want to know about doc or docx that is opened?

One thing I was wondering about is if I could alter the default program associated with .doc to mine, which is running as a service and then passing the details through it to mine to be opened? This seems like a bit of a hack so I was wondering if there was an easy way to hook into these sorts of file open?

UPDATE

From talking it through with various people here the most reliable way(and most resource effective) would seem to be to replace the existing file association for .doc. & .docx to my own program and then use Microsoft.Office.Interop.Word to launch word and then hook into the DocumentOpen event.

That way I get the file name thats being opened along with any future documents that are open in word.


回答1:


If I recall correctly, the temporary file that is created in the same folder has the file name format of ~$ + filename, for example:

~$very_important_file.doc

It contains the name of the user that opened the file. Note that the file has the hidden attribute set.

This makes it quite easy to figure out which document is actually open and by whom.




回答2:


Such tasks are usually accomplished using filesystem filter drivers. Procmon works this way. You can create your own filter driver or use the precreated one (eg. our CallbackFilter).



来源:https://stackoverflow.com/questions/26796897/filesystem-hook-for-doc-open-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!