Click here to Skip to main content
15,886,110 members
Articles / Web Development / HTML

Synchronicity - A Folder Synchronizing Application

Rate me:
Please Sign up or sign in to vote.
4.90/5 (29 votes)
7 Feb 2011CPOL25 min read 76.5K   3.5K   106  
Windows service for synchronizing folders
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceModel;
using System.ServiceProcess;


namespace SynchroService
{
	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	/// <summary>
	/// This class overrides the ServiceInstaller class so that we can start 
	/// the service automatically after it's been installed (if we want to). 
	/// </summary>
	public class ExtendedServiceInstaller : ServiceInstaller
	{
		public override void Commit(System.Collections.IDictionary savedState)
		{
			// Let the base commit do its job first.
			base.Commit(savedState);

			// Now start the newly installed service.
			ServiceController controller = new ServiceController(this.ServiceName);
			controller.Start();
			controller.Close();
		}
	}


	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	[RunInstallerAttribute(true)]
	public partial class SyncSvcInstaller : Installer
	{
		private ServiceInstaller m_serviceInstaller;
		private ServiceProcessInstaller m_processInstaller;

		public SyncSvcInstaller()
		{
			InitializeComponent();

			try
			{
				// create and configure our installer and processinstaller objects
				m_serviceInstaller = new ServiceInstaller();

				// If you don't want the service to start automatically, change the 
				// following line to whatever start mode is appropriate.
				m_serviceInstaller.StartType = ServiceStartMode.Manual;

				//TODO: Change these three items to more accurately reflect the service's purpose
				m_serviceInstaller.DisplayName = "Synchronicity Service";
				m_serviceInstaller.ServiceName = "Synchronicity Service";
				m_serviceInstaller.Description = "Synchronizes files between specified folder pairs";

				m_processInstaller         = new ServiceProcessInstaller();
				m_processInstaller.Account = ServiceAccount.LocalSystem;

				// add our installers to the list
				this.Installers.Add(m_serviceInstaller);
				this.Installers.Add(m_processInstaller);

				// perform any other preparatory steps necessary to make your service 
				// function properly.
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions