namespace ResRepricerImportFolderMonitor >>{ >> public partial class ResRepricerImportFolderMonitor : ServiceBase >> { >> public enum ServiceState >> { >> SERVICE_STOPPED = 0x00000001, >> SERVICE_START_PENDING = 0x00000002, >> SERVICE_STOP_PENDING = 0x00000003, >> SERVICE_RUNNING = 0x00000004, >> SERVICE_CONTINUE_PENDING = 0x00000005, >> SERVICE_PAUSE_PENDING = 0x00000006, >> SERVICE_PAUSED = 0x00000007, >> } >> >> [StructLayout(LayoutKind.Sequential)] >> public struct ServiceStatus >> { >> public long dwServiceType; >> public ServiceState dwCurrentState; >> public long dwControlsAccepted; >> public long dwWin32ExitCode; >> public long dwServiceSpecificExitCode; >> public long dwCheckPoint; >> public long dwWaitHint; >> }; >> >> [DllImport("advapi32.dll", SetLastError = true)] >> private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus); >> >> private FileSystemWatcher watcher = null; >> >> public ResRepricerImportFolderMonitor() >> { >> InitializeComponent(); >> } >> >> protected override void OnStart(string[] args) >> { >> // Update the service state to Start Pending. >> ServiceStatus serviceStatus = new ServiceStatus(); >> serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING; >> serviceStatus.dwWaitHint = 100000; >> SetServiceStatus(this.ServiceHandle, ref serviceStatus); >> >> // Create a new FileSystemWatcher with the path >> //and text file filter >> >> string CSVFolder = ConfigurationManager.AppSettings["CSVFolder"]; >> string CSVFolderProcessed = ConfigurationManager.AppSettings["CSVFolderProcessed"]; >> >> FileSystemWatcher watcher = new FileSystemWatcher(CSVFolder, "*.csv"); >> >> //Watch for changes in LastAccess and LastWrite times, and >> //the renaming of files or directories. >> watcher.NotifyFilter = NotifyFilters.LastAccess >> | NotifyFilters.LastWrite >> | NotifyFilters.FileName >> | NotifyFilters.DirectoryName; >> >> // Add event handlers. >> //watcher.Changed += new FileSystemEventHandler(OnChanged); >> watcher.Created += new FileSystemEventHandler(OnChanged); >> //watcher.Deleted += new FileSystemEventHandler(OnChanged); >> //watcher.Renamed += new RenamedEventHandler(OnRenamed); >> >> // Begin watching. >> watcher.EnableRaisingEvents = true; >> >> // Update the service state to Running. >> serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING; >> SetServiceStatus(this.ServiceHandle, ref serviceStatus); >> >> LogEvent("started"); >> } >> >> protected override void OnStop() >> { >> watcher.EnableRaisingEvents = false; // error occurs here >> watcher.Dispose(); >> >> LogEvent("stopped"); >> }>