Link to home
Start Free TrialLog in
Avatar of thegreekitalian
thegreekitalian

asked on

Convert Console App to Service

I have a little  FileSystemWatcher application that I wrote and would like to turn into a Windows Service.  Currently I'm compiling as a 'Windows Application' (to keep it windowless).  How can I convert this over to a service?  Note:  I've never written a service before.  My current code is below.
// #define TestMode
 
using System;
using System.IO;
 
namespace ProjectWatch
{
    class Program
    {
        static void Main(string[] args)
        {
            FileSystemWatcher projectWatcher = new FileSystemWatcher();
            projectWatcher.Path = @"I:\";
            projectWatcher.EnableRaisingEvents = true;
            projectWatcher.IncludeSubdirectories = true;
 
            projectWatcher.NotifyFilter = NotifyFilters.LastAccess | 
                                          NotifyFilters.LastWrite |
                                          NotifyFilters.DirectoryName | 
                                          NotifyFilters.FileName;
 
            projectWatcher.Deleted += new FileSystemEventHandler(OnChanged);
            projectWatcher.Created += new FileSystemEventHandler(OnChanged);
            projectWatcher.Renamed += new RenamedEventHandler(OnRenamed);
            
            while (Console.Read() != 'q') ;
        }
        
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            #if (TestMode)
                Console.WriteLine(DateTime.Now);
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
                Console.WriteLine("\n");
            #else
                if (e.FullPath.EndsWith(".tmp"))    
                { return; }
                StreamWriter pwLogWrite = File.AppendText(@"C:\projectWatch.log");
                pwLogWrite.WriteLine(DateTime.Now);
                pwLogWrite.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
                pwLogWrite.WriteLine();
                pwLogWrite.Close();
                pwLogWrite = null;
            #endif
        }
 
        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            #if (TestMode)
                Console.WriteLine(DateTime.Now);
                Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
                Console.WriteLine("\n");
            #else
                if (e.FullPath.EndsWith(".tmp"))
                { return; }    
                StreamWriter pwLogWrite = File.AppendText(@"C:\projectWatch.log");
                pwLogWrite.WriteLine(DateTime.Now);
                pwLogWrite.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
                pwLogWrite.WriteLine();
                pwLogWrite.Close();
                pwLogWrite = null;
            #endif
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thegreekitalian
thegreekitalian

ASKER

Ok, attached is a skeleton service.  Where would I paste my code?  Also, to keep the process running I was using while (Console.Read() != 'q') ; What would I use to keep the service actively watching the target?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
 
namespace ProjectGuardian
{
    public partial class ProjectGuardian : ServiceBase
    {
        public ProjectGuardian()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
 
        }
 
        protected override void OnStop()
        {
        }
    }
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jaime,

That's a great tutorial!  Thank you.  I'm still not quite sure where to add what I need to add, though.  I have already written the code, I just need to get it where it needs to be (and probably modified a bit).  I will also add the installer, etc (thanks again for that).  
When your service is started OnStart will be called.  OnStart will in turn do any necessary setup work (e.g. make sure I:\ exists), and start a new thread.  The new thread will be what was your main(); it'll have an empty for(;;) loop, or while(true).  You'll need to be able to signal your worker thread to exit when OnStop is called.  

Just a starting point, you'll need to see to other details, as Jaime pointed out.