Take the 2-minute tour ×

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?

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

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:

protected override void OnStart(string[] args)
FileSystemWatcher Watcher = new FileSystemWatcher("PATH HERE");
Watcher.EnableRaisingEvents = true;
Watcher.Changed += new FileSystemEventHandler(Watcher_Changed);

// This event is raised when a file is changed
private void Watcher_Changed(object sender, FileSystemEventArgs e)
// your code here

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.

share|improve this answer
So I should not add any items from the toolbox in the service windows.. Thanks! I will try it asap.. That was really helpful –  Philip Sy Aug 17 '12 at 3:50
I'd stay away from it. When you are running a service, Windows naturally disables form integration and you have to configure the service after installation to allow it. If it is something that only your eyes sees, then it probably isn't a big deal but it's a good habit to avoid that style of development (for security reasons as well). –  Luke Wyatt Aug 17 '12 at 3:56
UPDATE: In Windows Vista & Win 7, the option to allow desktop interaction was removed completely. Didn't realize this at posting time. –  Luke Wyatt Aug 20 '12 at 16:33
add comment

You can simply enable your filesystemwatcher object in the OnStart method by setting

EnableRaisingEvents = true;

Then handle the event. That should do it.

share|improve this answer
default value would be true –  Peru Aug 17 '12 at 4:23
Maybe. MSDN - "true if the component is enabled; otherwise, false. The default is false. If you are using the component on a designer in Visual Studio 2005, the default is true." –  Kevin Anderson Aug 17 '12 at 14:29
add comment

you can create a delegate to handle what has changed like

myWatcher.Changed += new FileSystemHandler(FSWatcherTest_Changed);

private void FSWatcherTest_Changed(object sender, 
                System.IO.FileSystemEventArgs e)
    //code here for newly changed file or directory

And so on

I would recommend you to read this article http://www.codeproject.com/Articles/18521/How-to-implement-a-simple-filewatcher-Windows-serv

Also have this delegate on_start in windows service

share|improve this answer
add comment

Your Answer


