title: Directory scanner Windows Service with FileSystemWatcher
body: <p>I'm trying to create a windows service that will grab new files uploaded to directory, edit them and move to other directory. It seems I can COPY them but not MOVE. Why is that?</p>
  
  <pre><code>using System;
  using System.ServiceProcess;
  using System.Threading;
  using System.IO;
  
  namespace ImportService
  {
      public class ImportServer : ServiceBase
      {        
          private System.Diagnostics.EventLog eventLog1;
          private FileSystemWatcher watcher;
  
          public ImportServer()
          {
              this.ServiceName = "ImportService";
  
              this.CanHandlePowerEvent = true;
              this.CanHandleSessionChangeEvent = true;
              this.CanPauseAndContinue = true;
              this.CanShutdown = true;
              this.CanStop = true;
              this.AutoLog = true;           
  
              InitializeComponent();
              if (!System.Diagnostics.EventLog.SourceExists("ImportServiceLogSource"))
                  System.Diagnostics.EventLog.CreateEventSource("ImportServiceLogSource", "ImportServiceLog");
              eventLog1.Source = "ImportServiceLogSource";
              eventLog1.Log = "ImportServiceLog";
          }
  
          public static void Main()
          {
              ServiceBase.Run(new ImportServer());            
          }        
  
          protected override void OnStart(string[] args)
          {
              //base.OnStart(args);
  
              eventLog1.WriteEntry("service started");
  
              watcher = new FileSystemWatcher();
              watcher.Path = "C:\\INPUT\\";
              watcher.Filter = "*.jpg";
              watcher.EnableRaisingEvents = true;
              watcher.Created += new FileSystemEventHandler(OnCreated);            
          }
  
          private void OnCreated(object sender, FileSystemEventArgs e)
          {
              String output_dir = "C:\\OUTPUT\\";
              String output_file = Path.Combine(output_dir, e.Name);
              File.Move(e.FullPath, output_file);
              // File.Copy() works here
              eventLog1.WriteEntry("moving file to " + output_file);
          }
  
          protected override void OnStop()
          {            
              eventLog1.WriteEntry("service stopped");
              base.OnStop();
          }
  
          protected override void OnContinue()
          {
              base.OnContinue();
          }
  
          protected override void OnPause()
          {
              base.OnPause();
          }
  
          private void InitializeComponent()
          {
              this.eventLog1 = new System.Diagnostics.EventLog();
              ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
              ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
  
          }
      }
  }
  </code></pre>
  
  <p>Also should I keep the <code>base.OnStart();</code> etc. What does it really do?</p>
  
  <p><strong>UPDATE:</strong> How to move file that was created in the watched directory? <em>File already in use</em> exception problem.</p>