Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Real Time Folder Synchronization Window Service

3.92/5 (8 votes)
23 Apr 2009CPOL2 min read 126.5K   9.4K  
A window service that can copy and delete files across domains to keep 2 folders synchronized

Introduction

FileSyncService is a window service that can copy and delete files across domains to keep 2 folders synchronized in real time.
If you need to synchronize 2 window folders and they are all in windows system, this service will meet your need. Especially if your folders are located in different domains, other methods may not work for you.

The Problems

Consider a situation where your program needs to keep 2 folders in Windows file system always synchronized, then it has to satisfy the following requirements:

  • Real time: Any change in the source folder should be reflected in the destination folder immediately
  • Work in background, no maintenance needed
  • If the destination folder is located on other computer, even out of domain, the program should be able to get access to the destination folder
  • The remote access is done by providing valid identity. The user name and password should be stored in the config file and the password should be encrypted.

The Solution

The program here demonstrates how to addresses the above issues:

System.IO.FileSystemWatcher Class

This class is used to observe the source folder and raises event whenever any change is detected in the folder:

C#
//create FileSystemWatcher object 
FileSystemWatcher _watchFolder = new FileSystemWatcher();
string srcRoot = ConfigurationManager.AppSettings["localFolder"];
//define the properties of the file watcher
_watchFolder.Path = srcRoot;
_watchFolder.IncludeSubdirectories = true;
_watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
_watchFolder.NotifyFilter = 
	_watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
_watchFolder.NotifyFilter = 
	_watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;
// Now hook the triggers(events) to our handler (eventRaised)
_watchFolder.Changed += new FileSystemEventHandler(eventChangeRaised);
_watchFolder.Created += new FileSystemEventHandler(eventCreateRaised);
_watchFolder.Deleted += new FileSystemEventHandler(eventDeleteRaised);
_watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRenameRaised);
//enable the events
_watchFolder.EnableRaisingEvents = true;

Window Service

The program will run whenever the computer is up.

NetWorkDrive Class

This class is used to map network drive to create the communication channel. The drive name is dynamically searched for the availability in the local computer. When a change is detected by the file watcher, the NetWorkDrive object will map the network drive; after the synchronization is done and no activity for 5 seconds, the object will unmap the network drive to release the resource. A Timer class is used here.

SecurePassword Class

This class is used to encrypt the password in the config file when the window service is running first time.

How to Use the Window Service

Note

  • This program uses Win32 API to map network drive in remote folder. Please make sure there is no drive currently mapped to the destination folder from the local computer.
  • Make sure the user for accessing remote domain is a domain user.

Installation

  • Run the FileSyncServiceSetup.msi, follow the wizard to install the window service.
  • Open FileSync.exe.config located in the installation folder (default: C:\Program Files\Code Project\FileSyncServiceSetup).
  • Fill the required values. Note: make sure the user in remote domain is a domain user.
  • Start the window service named File Synchronization service.
  • After it is installed, open the local folder and add a new file; verify if the new file is also shown in the remote folder. 
  • Go to Computer Management -> System tools -> Event Viewer -> Application to view the related logs.

These codes are found in the internet .NET community. I just put them together. Feel free to use them.

History

  • 23rd April, 2009: Initial post

License

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