Here's how I implemented the class:
public partial class NXPFileWatcher : ServiceBase
{
FileSystemWatcher FSWatcher = null;
public NXPFileWatcher()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
FSWatcher = new FileSystemWatcher();
FSWatcher.Path = ConfigurationManager.AppSettings["WatchPath"];
FSWatcher.InternalBufferSize = 32768;
FSWatcher.NotifyFilter = NotifyFilters.CreationTime;
FSWatcher.Filter = "*.xml";
FSWatcher.Created += new FileSystemEventHandler(FSWatcher_Created);
FSWatcher.EnableRaisingEvents = true;
SendNotification("has Started");
}
catch (Exception ex)
{
Log.Write(ex.Message);
}
}
protected override void OnStop()
{
try
{
FSWatcher.Dispose();
SendNotification("has Stopped");
}
catch (Exception ex)
{
Log.Write(ex.Message);
}
}
protected override void OnPause()
{
OnStop();
}
protected override void OnContinue()
{
OnStart(null);
}
protected override void OnShutdown()
{
try
{
FSWatcher.Dispose();
SendNotification("has stopped due to system shutting down");
}
catch (Exception ex)
{
Log.Write(ex.Message);
}
}
private void FSWatcher_Created(object sender, FileSystemEventArgs e)
{
// Now open the file
XmlFile xml = new XmlFile();
xml.FullPath = e.FullPath;
xml.Process();
}
}