title: Using a FileSystemWatcher with Windows Service
body: <p>I have a windows service which needs to monitor a directory for files and then move it to another directory. I am using a FileSystemWatcher to implement this.</p>
  
  <p>This is my main Service class.</p>
  
  <pre><code>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()
      {
  
      }
  }
  </code></pre>
  
  <p>This is my FileProcessor Class</p>
  
  <pre><code>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);
      }
  }   
  </code></pre>
  
  <p>After I install and start the service, it works fine for 1 file and then stops automatically. What is making the service stop automatically? When I check the event log I don't see an error or warning!</p>