Where do I put a FileSystemWatcher in my mvc application?
-
13-09-2019 - |
Question
We have an asp.net mvc application that we would like to integrate a FileSystemWatcher with. I have seen many good examples on how to implement the FileSystemWatcher but I don't really know where to put it in my application. It seems like it should be started with the application. Any ideas?
Solution
Set it up in your Application_Start(). One of the best examples would be setting up log4net with ConfigureAndWatch(), which internally uses a FileSystemWatcher. Example:
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("/log4net.config")))
OTHER TIPS
You're probably better off using the FileSystemWatcher in conjunction with a Windows Service if you want to monitor the file system for changes. A Windows Service is continually running, whereas code in a web application only executes in response to a HTTP request.
This article may be a good place to start.