L
ljh
Has anyone else noticed that the FileSystemWatcher raises the changed event
twice when a file is changed?
Do you have any idea why this is the case?
twice when a file is changed?
Do you have any idea why this is the case?
Willy Denoyette said:|I saw those.....but there is no real solution listed there.
|
| This seems to be another of .Nets gotcha's. Something that sounds
great,
| but doesn't really work out in real world programming.
|
This has nothing to do with .NET, it's the way the FS signals events to
the
OS and the way the FileSystemWatcher's underlying Win32 API
(ReadDirectoryChangesW) presents FS event info to the caller.
Willy Denoyette said:|
| | >
| > | > |I saw those.....but there is no real solution listed there.
| > |
| > | This seems to be another of .Nets gotcha's. Something that sounds
| > great,
| > | but doesn't really work out in real world programming.
| > |
| >
| > This has nothing to do with .NET, it's the way the FS signals events
to
| > the
| > OS and the way the FileSystemWatcher's underlying Win32 API
| > (ReadDirectoryChangesW) presents FS event info to the caller.
|
| That's why I'd much rather be able to detect running instances of Word
and
| trap events like the saving of a file. Alas, that does not seem to be
| possible with .Net.
|
| I have seen tons of code that show how to create an instance of Word (or
| Excel or Access or Outlook) and manipulate it programatically, but not
one
| that can show how to detect a running instance of Word and basically
watch
| what the user is doing with the files.
|
| I am almost certain that this is not possible with managed code at all.
| Another capability wiped out in the name of "safe" code.
|
I said that FileSystemWatcher uses the underlying Win32 API
"ReadDirectoryChangesW", even if you use native code in you scenario you
will see two change events, Also ReadDirectoryChangesW is an API designed
to
watch "Directory" changes not File changes, so is FSW.
The reason why see two events is because when Word saves a document, it
first save the old contents of the doc .file into a .tmp file (a rename),
after which he copies the new document contents into a .tmp file and
renames
this .tmp back into your .doc file (a rename again). Another thing to keep
in mind is that Word maintains a lock file in the same directory as the
.doc
file, this lock file has the same name as the .doc file with ~$ prepended.
This file is created and changed when words opens a doc file, it
'deletes'this lock file when the .doc file gets closed. So it's just a
matter of carefully selecting your NotifyFilters, and have an idea about
what exactly is done by the applicatins accesing the files in the watched
directory.
| Although, I suppose it has to be that way. If Microsoft really seeks to
| save people (programmers) from themselves (which is a fool's game in any
| industry), they have to take away the tools that they mis-use to do so.
|
| You can't have "safe" and all-powerful in the same toolset. IMHO, it
looks
| like we're sacrificing power for "safety" with .Net.
|
Again, .NET has nothing to do with this, but if you don't believe it, no
one
stops you do it in native code.
I will need to do COM interop to bring in old code to accomplish part of
the
functionality that is not (as far as I can see) possible with .Net. I may
be missing something, but I do not see the ability in .Net to monitor
system-wide keystrokes or mouse events. Did I miss it somewhere?
Thanks for your response!
Lebesgue said:You can do a limited set of system hooks including keyboard and mouse
events in .NET see:
http://www.codeproject.com/csharp/GlobalSystemHook.asp?msg=840940
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.