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
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).
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.
Just a starting point, you'll need to see to other details, as Jaime pointed out.
ASKER
Open in new window