views:

253

answers:

4

Hello,

I need to create an application that when a user copies a file into an specific folder, my application will be activated and the file will be changed, also, when the user reads back any file in the folder, changes will also be made to the file.

Is it possible?

I'll use .net for this.

I think the folder C:\WINDOWS\assembly is somehow like this?

Any help will be appreciated! Thanks!

+3  A: 

Well, I'm sure you could write an explorer extension that would do that, but the more typical method that I know of is to write a windows service that monitors the directory in question using the FileSystemWatcher class to monitor events. The service can then perform the requested actions (or spawn an executable to do it).

One caveat I have noticed is that sometimes the file watcher can miss events if a lot of activity occurs at once (as of .net 2.0 - haven't checked 3.5).

If you use that method, make sure you check the directory contents yourself when you receive a change notification rather than relying on what the filewatcher tells you. I ended up combining the filewatcher event handling with an infrequent poll just to make sure I didn't miss anything.

Andrew Rollings
A: 

You can monitor for changes with FileSystemWatcher.

You can configure the component to watch either an entire directory and its contents or a specific file or set of files within a given directory. The FileSystemWatcher component raises an event whenever a file or subdirectory within the specified root directory is created, deleted, renamed, or changed in some other way. The types of changes that the component monitors include changes in the file's or subdirectory's attributes, size, last write time, last access time, and security settings.

Paul
A: 

The closest thing you'll get in .net is the FileSystemWatcher class, it will monitor for changes (creating and writing) but not for reading activity.

There are many samples online and on MSDN

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Y Low
+1  A: 

You can detect users copying files into a folder with a service that uses FileSystemWatcher. Altering the contents of a file when an app reads the file requires viral techniques that patch the operating system. Like a rootkit or a file system filter driver. Not something I'd recommend considering, alter the file after it was written.

c:\windows\assembly contains the GAC. .NET installs a shell extension handler that hides the contents of the folder. The folder is otherwise quite accessible, the shell extension handler only works when you look at the folder with Windows Explorer. Writing shell extension handlers with .NET is strongly discouraged by Microsoft, the framework also completely lacks support.

Hans Passant