1

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()
        {



        }
    }
}
METALHEAD
  • 2,734
  • 3
  • 22
  • 37

2 Answers2

0

Windows Services run under the LocalSystem account by default (unless you specify another account when you install them). You will need to grant this account access to both folders.

zmbq
  • 38,013
  • 14
  • 101
  • 171
0

Use the following command to create the service with username and password:

sc create "ServiceName" binPath= "G:\services\service.exe" DisplayName= "ServiceName" start= "auto" obj= "username"   password= "password"

Repace : ServiceName, exe file location, DisplayName, username and password.

Mobaruk H
  • 54
  • 1
  • 7
  • Hi.Thanks for your comment. Is there a way the above program to run without UserID and Password..? – METALHEAD May 22 '17 at 10:26
  • The service requires enough permission to access files which it is not getting in this scenario because the folder location is with the Admin user's private folder location. You can either move file location to a generic drive(other than your C drive) or use the admin username and password. – Mobaruk H May 22 '17 at 10:30
  • When you are running it as a console app it is using your username to access the file, hence it is working at that time. – Mobaruk H May 22 '17 at 10:32