Friday, January 28, 2011

Create windows service with file system watcher running in the background

1. First create an empty project.
2. Add a class and inherit from the  System.ServiceProcess.ServiceBase.
3. You might have to add references of System.dll and System.ServiceProcess.dll.
4. Change the class like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WsEg1
{
   public  class UserService1 : System.ServiceProcess.ServiceBase
    {
       public UserService1()
       {
           this.ServiceName = "MyService2";
           this.CanStop = true;
           this.CanPauseAndContinue = true;
           this.AutoLog = true;
       }
       public static void Main()
       {
           System.ServiceProcess.ServiceBase.Run(new UserService1());
       }
       protected override void OnStart(string[] args)
       {
           // Insert code here to define processing.
           //FileStream fs = new FileStream(@"C:\Users\sameer\Desktop\Skill Trac\ServiceInfo.txt", FileMode.OpenOrCreate);
           //StreamWriter sw = new StreamWriter(fs);
           //sw.WriteLine("Service has been started at " + System.DateTime.Now);
           //sw.Close();
           //fs.Close();
           FileWatcher.Run();
       }

       protected override void OnStop()
       {
           //FileStream fs = new FileStream(@"C:\Users\sameer\Desktop\Skill Trac\ServiceInfo.txt", FileMode.OpenOrCreate);
           //StreamWriter sw = new StreamWriter(fs);
           //sw.WriteLine("Service has been stopped at " + System.DateTime.Now);
           //sw.Close();
           //fs.Close();
           FileWatcher.Close();
       }

       private void InitializeComponent()
       {
           //
           // UserService1
           //
           this.ServiceName = "SamService";

       }




    }
}

5. Add ProjectInstaller by opening Service in design view, right click and add installer.
6. After that add setup project and add project output to be primary output of solution.
7. Thats it you are done.

Below is the code of FileWatcher Class, which performs the snooping of folders :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WsEg1
{
    public class FileWatcher
    {
        public static void Run()
        {
            FileSystemWatcher Watcher = new FileSystemWatcher(Environment.GetEnvironmentVariable("USERPROFILE"));
            Watcher.Path = @"C:\Users\sameer\Desktop\Skill Trac";
            Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            Watcher.Filter = "*.txt";

            //Add event handlers for all the events
            Watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
            Watcher.Renamed += new RenamedEventHandler(Watcher_Renamed);
            Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);

            Watcher.EnableRaisingEvents = true;
           

        }

        public static void Close()
        {
           
           
        }

        static void Watcher_Deleted(object sender, FileSystemEventArgs e)
        {
           
            //FileStream fs = new FileStream(@"C:\Users\sameer\Desktop\Skill Trac\ServiceInfo.txt", FileMode.OpenOrCreate);
            //StreamWriter sw = new StreamWriter(fs);

            StreamWriter sw = File.AppendText(@"C:\Users\sameer\Desktop\Sam Sync\ServiceInfo.txt");
           
            sw.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);
            sw.Close();
            //fs.Close();
       
        }

        static void Watcher_Renamed(object sender, RenamedEventArgs e)
        {

            StreamWriter sw = File.AppendText(@"C:\Users\sameer\Desktop\Sam Sync\ServiceInfo.txt");

            sw.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);
            sw.Close();
        }

        static void Watcher_Changed(object sender, FileSystemEventArgs e)
        {
            StreamWriter sw = File.AppendText(@"C:\Users\sameer\Desktop\Sam Sync\ServiceInfo.txt");

            sw.WriteLine("File : " + e.FullPath + " Change Type is : " + e.ChangeType);
            sw.Close();
        }
    }
}

No comments:

Post a Comment