File System Watcher
-
Hey all, I currently developing an application where i need to "keep an eye" on certain files on any given folder... I am doing the following: System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher(); watcher.Path = @path; watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime; watcher.IncludeSubdirectories = true; watcher.Filter = ".txt"; watcher.EnableRaisingEvents = true; watcher.Created+=new System.IO.FileSystemEventHandler(watcher_Created); watcher.Deleted +=new System.IO.FileSystemEventHandler(watcher_Deleted); // the events handlers watcher_Created(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Created: "+ e.Name); System.IO.File.Delete(e.FullPath); } watcher_Deleted(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Deleted: "+ e.Name); } However it seems that i am missing something here because I am creating a file in the given path but the events are not been triggered... What am i missing? Oh, i would also i like to know if a file is renamed how can i changed it back and if it is deleted how can i restore it? Thanks all!
Luis E Tineo S
-
Hey all, I currently developing an application where i need to "keep an eye" on certain files on any given folder... I am doing the following: System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher(); watcher.Path = @path; watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime; watcher.IncludeSubdirectories = true; watcher.Filter = ".txt"; watcher.EnableRaisingEvents = true; watcher.Created+=new System.IO.FileSystemEventHandler(watcher_Created); watcher.Deleted +=new System.IO.FileSystemEventHandler(watcher_Deleted); // the events handlers watcher_Created(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Created: "+ e.Name); System.IO.File.Delete(e.FullPath); } watcher_Deleted(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Deleted: "+ e.Name); } However it seems that i am missing something here because I am creating a file in the given path but the events are not been triggered... What am i missing? Oh, i would also i like to know if a file is renamed how can i changed it back and if it is deleted how can i restore it? Thanks all!
Luis E Tineo S
kingletas wrote:
watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime;
You are missing in this line. your are notifying only the Creation time, but not file name !!! :) Try this one
mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess ;
This will work.. and let me know when done !!!! Good luckBest Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"
-
kingletas wrote:
watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime;
You are missing in this line. your are notifying only the Creation time, but not file name !!! :) Try this one
mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess ;
This will work.. and let me know when done !!!! Good luckBest Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"
Thanks for replying but it seems that i am doing something terribly wrong:( because it is not working yet and to make sure i modified the notifier like this: watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.Attributes | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.Security| System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite; but no luck yet, maybe i need to add something else? What do you think?
Luis E Tineo S
-
Thanks for replying but it seems that i am doing something terribly wrong:( because it is not working yet and to make sure i modified the notifier like this: watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.Attributes | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.Security| System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite; but no luck yet, maybe i need to add something else? What do you think?
Luis E Tineo S
can you infrom me actually what error you are getting ??? check this one......
private void File_Watcher(string sFolderPath) { FileSystemWatcher mywatcher = new FileSystemWatcher(sFolderPath); mywatcher.Filter = ""; mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess ; mywatcher.EnableRaisingEvents = true; mywatcher.IncludeSubdirectories = true; mywatcher.Created += new FileSystemEventHandler(mywatcher_created); mywatcher.Deleted += new FileSystemEventHandler(mywatcher_deleted); mywatcher.Changed += new FileSystemEventHandler(mywatcher_changed); mywatcher.Renamed += new RenamedEventHandler(mywatcher_renamed); }
Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"
-
Thanks for replying but it seems that i am doing something terribly wrong:( because it is not working yet and to make sure i modified the notifier like this: watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.Attributes | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.Security| System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite; but no luck yet, maybe i need to add something else? What do you think?
Luis E Tineo S
I found what i was doing wrong :-).... on my desperation to get it to work i added: Watcher.BeginInit() when the form loaded, but once i removed that line it worked well... now any tip on how to restore it the files to what they were before any changes were made... like if renamed restore the file and if delete it restore it as well... I think a better question would be how can i backup an entire folder or how to store these files into a database without causing perfomance issues? -- modified at 0:53 Friday 16th November, 2007
Luis E Tineo S
-
I found what i was doing wrong :-).... on my desperation to get it to work i added: Watcher.BeginInit() when the form loaded, but once i removed that line it worked well... now any tip on how to restore it the files to what they were before any changes were made... like if renamed restore the file and if delete it restore it as well... I think a better question would be how can i backup an entire folder or how to store these files into a database without causing perfomance issues? -- modified at 0:53 Friday 16th November, 2007
Luis E Tineo S
kingletas wrote:
I think a better question would be how can i backup an entire folder or how to store these files into a database without causing perfomance issues?
For , that you have to use Background Worker.
Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"