views:

208

answers:

2

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?

+2  A: 

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.

pmarflee
+1 Good answer on several levels.
Robert Harvey
I don't know why so many people upvoted this, this is plain wrong. ASP.NET applications can run background threads without any problems. And they can respond to FileSystemWatcher events without any request triggering the code.
Mauricio Scheffer
@Mauricio: I don't think you can say my answer is 'wrong'. I've presented a workable solution to the OP's question. I never said this was the only solution, there are alternatives, however IMHO this represents the best approach.
pmarflee
"code in a web application only executes in response to a HTTP request" -> not true
Mauricio Scheffer
+1  A: 

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")))
Mauricio Scheffer