简体   繁体   中英

filesystemwatcher as windows service?

I am setting out to create an app that will watch a directory for any files created. pretty straightforward time to use a filesystemwatcher. My question relates to how to utilize it. Is it common practice to use a windows service to ensure the application is always running?

i have been trying to get away from building windows services if i don't have to, but i really don't see an alternative to doing it that way in this instance. normally, i would convert my service to a console app and schedule it using windows scheduler, but that doesn't really apply in this situation.

can anyone recommend a better way of implementing the filesystemwatcher than a windows service?

thanks for any thoughts.

EDIT in response to the comments below, more specifically, i just have to watch a directory on a server, and when a new file is created, i have to move a copy of that file into a different directory on the same server, perhaps renaming it in the process.

The frequency and amount of files will be quite small. perhaps 5-10 at most in a day.

I'm not sure yet how the file watcher works, but this is what I'm thinking: The file system fires events; I mean like NTFS must be doing that. Your file watcher hooks into those events. The file watcher probably suspends the thread it's running in until an event occurs and the event somehow wakes up the thread. A suspended thread uses pretty much very few cpu cycles (actually none) while it is suspended, so waiting for a file event costs nothing. So a polled approach wastes mucho beaucoup (that's French, it means 'a shit load') of cpu cycles but the file watcher does not. You could probably look at PerfMon to see if this is likely true.

You should describe more about what you want to do, but typically if you have something that needs to run in the background and does not require direct user interaction, then a service often makes sense.

You can use Remoting to connect a front-end to your service as needed if you'd like.

Yes, use a service for this kind of operation, but don't use filesystem watcher . If you poll for files in your service, dont use the Timer class either.

Do check to make sure the file is completed writing and is no longer locked before trying to move it.

Its trivial to poll for file changes (syntax may be off), and eliminates much of the extra programming associated with file system watcher events.

While True 'or your exit condition'
 Dim _files() as FileInfo = Directory.GetFiles(yourTargetDirectory)
 For Each _file as FileInfo In _files
  If _file.LastModifiedDate < DateTime.Now.AddMinutes(1) Then
    'move your files'
  End If
 Next

End While

Using a Windows service to wrap FileSystemWatcher is perfectly fine for what you need.

FSW is the right tool for the job (native code for filesystem watching is a bear to get right), and a service is the right mechanism to deploy it given you need 'always on' operation.

The service credentials will be independent of logged-in user too, which may be useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM