I've built a windows service using FileWatcher
class. It has to detect the incoming files in a folder and move them to the new location.
My code is running fine as console application but not working as Windows service.The files are staying in the Source folder only. They are not moving to the destination.I've given full access to the folders and service running in LOCAL SYSTEM account. Can you please point out the error. Thanks in advance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
bulk_watch();
}
public static void bulk_watch()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Users\ADMIN\Downloads\FW_Source";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
public static void OnChanged(object sender, FileSystemEventArgs e)
{
DirectoryInfo directory = new DirectoryInfo(@"C:\Users\IBM_ADMIN\Downloads\FW_Source");
FileInfo[] files = directory.GetFiles("*.*");
foreach (var f in files)
{
File.Move(f.FullName, System.IO.Path.Combine(@"C:\Users\ADMIN\Downloads\FW_Dest", Path.GetFileName(f.FullName)));
}
}
protected override void OnStop()
{
}
}
}