public FileInputMonitor() { DirectoryInfo dir = new DirectoryInfo(folderToWatchFor); if (dir.Exists) { fileSystemWatcher = new FileSystemWatcher(folderToWatchFor); // Instruct the file system watcher to call the FileCreated method // when there are files created at the folder. fileSystemWatcher.Created += new FileSystemEventHandler(FileCreated); fileSystemWatcher.EnableRaisingEvents = true; } else { Library.WriteErrorLog("watch folder directory doesn't exists"); Library.LogEvent("watch folder directory doesn't exists at " + DateTime.Now.ToString()); } } // end FileInputMonitor()
protected override void OnStop() { fileInputMonitor.OnStopFileMonitoring(); Library.WriteErrorLog("Service is stopping for file watching"); Library.LogEvent("Service is stopping for file watching at " + DateTime.Now.ToString()); }
//for running as console app then need to exclude Program.cs and change project properties //private static void Main(string[] args) //{ // var service = new FileInputMonitorService(); // if (Environment.UserInteractive) // { // service.OnStart(args); // Console.WriteLine("Press any key to stop the program"); // Console.Read(); // service.OnStop(); // } // else // { // Run(service); // } //} protected override void OnStart(string[] args) { Library.WriteErrorLog("Service is started for file watching"); Library.LogEvent("Service is started for file watching at " + DateTime.Now.ToString()); fileInputMonitor = new FileInputMonitor(); }