I have to create a program that monitors changes in file size. I already made a simple windows service and filesystemwatcher so I am now familiar w/ the concept. I also made a code that checks for the filesize (made it in a form button)but haven't yet implemented in my filesystemwatcher. How do I create a windows service that has a filewatcher that monitors the file size? Do I have to put a filesystemwatcher inside the windows service and call the watcher via the OnStart method?
C# – Windows Service w/ FileSystemWatcher in C#
cfilesystemwatcherwindows-services
Related Topic
- C# – FileSystemWatcher doesn’t recognize Excel SaveAs
- C# – FileSystemWatcher Changed event is raised twice
- Windows – Create Windows service from executable
- C# – filesystemwatcher as windows service
- C# – Windows service on Local Computer started and then stopped error
- How to solve “The specified service has been marked for deletion” error
Best Answer
If you're making a Window's service, then you'll want to do it programmatically. I usually keep forms out of my services and make a separate interface for them to communicate. Now the FileSystemWatcher doesn't have an event to watch solely for size, so you'll want to make a method that ties to FileSystemWatcher.Changed to check for modifications to existing files. Declare and initialize the control in your OnStart method and tie together the events as well. Do any cleanup code in your OnStop method. It should look something like this:
Also note, the FileSystemWatcher will fire off multiple events for a single file, so when you're debugging watch for patterns to work around it.