c# - Using a FileSystemWatcher with Windows Service -


i have windows service needs monitor directory files , move directory. using filesystemwatcher implement this.

this main service class.

public partial class sqlprocessservice : servicebase {     public sqlprocessservice()     {         initializecomponent();     }      protected override void onstart(string[] args)     {         fileprocesser fp = new fileprocesser(configurationmanager.appsettings["frompath"]);         fp.watch();     }      protected override void onstop()     {      } } 

this fileprocessor class

public class fileprocesser {     filesystemwatcher watcher;     string directorytowatch;     public fileprocesser(string path)     {         this.watcher = new filesystemwatcher();         this.directorytowatch = path;     }     public void watch()     {          watcher.path = directorytowatch;         watcher.notifyfilter = notifyfilters.lastaccess |                      notifyfilters.lastwrite |                      notifyfilters.filename |                      notifyfilters.directoryname;         watcher.filter = "*.*";         watcher.changed += new filesystemeventhandler(onchanged);         watcher.created += new filesystemeventhandler(onchanged);         watcher.enableraisingevents = true;     }      private void onchanged(object sender, filesystemeventargs e)     {         file.copy(e.fullpath, configurationmanager.appsettings["topath"]+"\\"+path.getfilename(e.fullpath),true);         file.delete(e.fullpath);     } }    

after install , start service, works fine 1 file , stops automatically. making service stop automatically? when check event log don't see error or warning!

your local variable fp disposed off once goes out of scope. solution declare instance variable instead


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -