1

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • Note that if you re-associate .doc / docx to your own app, you won't be notified if the user opens Word and then uses the Word UI to select the file and open it. – NSFW Nov 07 '14 at 08:43
  • ProcMon is able to do what you need. So then the question is, how does ProcMon do it? I don't know, but the answer may be out there somewhere. – NSFW Nov 07 '14 at 08:49
  • Hrm good catch I didn't think about file open from within word, you're sure it doesn't still do a lookup? I don't suppose it would really. Lemmy have a look at procmon – Mrk Fldig Nov 07 '14 at 09:00
  • Hmm actually surely if you opened word through the interop assembly you could hook into the fileopen dialogue no? – Mrk Fldig Nov 07 '14 at 14:06
  • I'm not familiar with Word interop, but it sounds promising. – NSFW Nov 07 '14 at 20:35

2 Answers2

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.

Darko Kenda
  • 4,781
  • 1
  • 28
  • 31
  • I thought that'd be the case but it renames the first two characters to ~$ so it's not 100% reliable for determining whats been opened – Mrk Fldig Nov 07 '14 at 08:58
  • If the file name (without extension) is shorter than 7 characters, it prepends the ~$, if the file is longer it starts replacing the first or the first two characters. If you have multiple files with file names longer than 8 characters that differ in the first two chars only, that could be a problem (Word only creates one temp file for both documents in those cases) – Darko Kenda Nov 07 '14 at 09:40
  • Hrm thats a possibility I suppose, i'm surprised there isn't an easy way to hook into file open though – Mrk Fldig Nov 07 '14 at 13:11
1

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).

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
  • Thanks for this for the alpha I'm using a fs watcher on the c:\users\whatever but yes I think the filter driver would be the way to go for production – Mrk Fldig Mar 07 '15 at 08:19