Windows service with file system in C #

I need to create a program that controls file size changes. I already made a simple Windows service and file system, so now I am familiar with the concept. I also made code that checks the file size (made it in the form), but haven't implemented it on my file system yet. How to create a windows service with a file element that controls file size? Should I put the file system in the Windows service and call the observer using the OnStart method?

+8
c # windows-services filesystemwatcher
source share
3 answers

If you are creating a Window service, then you will want to do it programmatically. I usually leave forms outside my services and create a separate interface for communication. Now FileSystemWatcher does not have an event to view only for size, so you will want to make a method related to FileSystemWatcher.Changed to check for changes to existing files. Declare and initialize the control in the OnStart method and bind events as well. Do any cleanup code in the 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 that FileSystemWatcher fires several events for a single file, so when you debug the clock for patterns to get around it.

+14
source share

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

 EnableRaisingEvents = true; 

Then handle the event. That should do it.

+3
source share

you can create a delegate to handle what has changed as

 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 read this article http://www.codeproject.com/Articles/18521/How-to-implement-a-simple-filewatcher-Windows-serv

Also have this on_start delegate in windows service

+1
source share

All Articles