Posted By: TimP | Oct 31st, 2006 @ 12:19 PM
page 1 of 1
Comments: 8 | Views: 5250

Is there any way to be notified when files are opened by a user? Something like the FileSystemWatcher class, except with an "Opened" event. I just need the file name of the file that's opened.

For example, User double clicks on "myfile.txt" on the Desktop and Notepad is launched. I need some way to be notified that C:\Documents and Settings\User\Desktop\myfile.txt has been opened.

Thanks!

TommyCarlier
TommyCarlier
I want my scalps!
You can set the NotifyFilter-property to LastAccess, and catch the Changed-event. Like this:
FileSystemWatcher lWatcher = new FileSystemWatcher(@"C:\Documents and Settings\User\Desktop", "myfile.txt");
lWatcher.NotifyFilter = NotifyFilters.LastAccess;
lWatcher.Changed += new FileSystemEventHandler(HandlerWatcherChanged);

void HandlerWatcherChanged(object sender, FileSystemEventArgs e)
{
   // file has been accessed
}
Minh
Minh
WOOH! WOOH!
Just a note. FileSystemWatchers don't work on Windows 9x.
JohnAskew
JohnAskew
9 girl in pink sweater
TommyCarlier wrote:
You can set the NotifyFilter-property to LastAccess, and catch the Changed-event. Like this:
FileSystemWatcher lWatcher = new FileSystemWatcher(@"C:\Documents and Settings\User\Desktop", "myfile.txt");
lWatcher.NotifyFilter = NotifyFilters.LastAccess;
lWatcher.Changed += new FileSystemEventHandler(HandlerWatcherChanged);

void HandlerWatcherChanged(object sender, FileSystemEventArgs e)
{
   // file has been accessed
}



Groovy. I was wondering about that ability but hadn't tried to find it.

Tommy is my code example hero.
Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.
Minh wrote:
Just a note. FileSystemWatchers don't work on Windows 9x.


Just another note. FileSystemWatchers don't work too well anywhere else as well.

Be careful if you're monitoring lots of changes at once. I had lots of problems with multiple events popping for single operations, events coming up in the wrong order and so forth. Shouldn't be a problem if you have a second or two between events.
Yggdrasil wrote:

Be careful if you're monitoring lots of changes at once. I had lots of problems with multiple events popping for single operations, events coming up in the wrong order and so forth. Shouldn't be a problem if you have a second or two between events.


I've seen this too, primarily with file creation events - typically I have had to keep track of the recent events and ignore dupes - also trying to get an exclusive lock on the file to ensure that the file copy (or write) has actually finished because the creation event is raised before the file is completely there.
TommyCarlier
TommyCarlier
I want my scalps!
Yeah, the FileSystemWatcher has quite a few quirks. At work, I've created a wrapper class around it to more reliably detect created files, and make sure they are no longer locked when you try to read the files.
CRG1970
CRG1970
.NET To the Moon

Tommy, high.

This appears to be a very common problem.  I can see you answered this a while back, but was wondering if you could share with us more info on how you created that wrapper.  I think many of us will find it very, very useful.

page 1 of 1
Comments: 8 | Views: 5250
Microsoft Communities