FileWatcher Control

  • Thread starter Thread starter Stephen
  • Start date Start date
S

Stephen

Hi ,

I am new to FileSystemWatcher Control and observed something that I dont
quite understand.

If I add file1 and then file2, If i have a FW_Created event, then it
displays the messagebox two times, and then if I have all other events, then
all the other events are fired why?

Thanks,
Stephen
 
Hi Stephen,

The file watcher events can get complicated, especially depending on how you
are creating the file.

For instance, if you use your context menu(right click->New->Text Document,)
it actually does a number of file operations.
It will first create a file called "New Document.txt" (that counts as a file
creation). It then "renames" the file to whatever you type in, that counts as
both a "delete" file event and a "create" file event, because right after you
hit your enter to finish renaming it actually deletes the "new document.txt"
and creates a new "whatever you named it.txt"

So, can you explain how you are creating the new files? Also, take a really
good look at the FileWatcher help files. It explains quite a bit of that in
pretty concise language(probably much better than I can explain it)

hth,

Seg
 
Hi Seg,

thats precicely what happened... I tried creating a new file from scratch,
File->Notepad
Save As -> (location) and it gave me messages to all events

is there a way i can avoid it or thats the way it is and I have to work
around?

Thanks,
Stephen
 
As pointed out, these events are somewhat mystifying, although I've learned
that it's "not a bug". here's some example code of how I handle this issue:

private DateTime lastChangeTime;

private string lastFilePath;



private void watcher_Changed(object sender, FileSystemEventArgs e)

{

// handle issue with FileSystemWatcher where multiple events are
fired.

// P.S. It's not necessarily a "BUG", its just that we don't
want this behavior.

TimeSpan span = DateTime.Now.Subtract(lastChangeTime);

if (span.TotalSeconds > 2 || lastFilePath != e.FullPath)

{

// wait a second for any locks to be released

// before taking actions

Thread.Sleep(1000);

EventLog.WriteEntry("TestLib", "Cache Invalidated at " +
e.FullPath + ": " + e.ChangeType + DateTime.Now.ToString());

// you can have a switch statement here for various
different cache items,

// each with its own different monitored file name...

if (e.FullPath.ToLower().IndexOf("mycache.txt") > -1)

{

ReloadMyCache();

// update bugHelpers

lastChangeTime = DateTime.Now;

lastFilePath = e.FullPath;

}



if (e.FullPath.ToLower().IndexOf("myothercache.txt") > -1)

{

ReloadMyOtherCache();

// update bugHelpers

lastChangeTime = DateTime.Now;

lastFilePath = e.FullPath;

}

}


--Peter
 
Hi Peter,

Thanks for the reply but I still dont know how to do this?
Can i get some sample code? I am new to FileSystemWatcher
I cant understand your code snippet,
what is "mycache.txt" -- is that what i am monitoring and what is
"myothercache.txt"
what are ReloadMyCache and ReloadMyOtherCache

Sorry for the inconvenience

Thanks,
Stephen
 

Ask a Question

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.

Ask a Question

Back
Top