Click here to Skip to main content
13,549,436 members
Click here to Skip to main content
Add your own
alternative version


62 bookmarked
Posted 23 Apr 2009
Licenced CPOL

Real Time Folder Synchronization Window Service

, 23 Apr 2009
Rate this:
Please Sign up or sign in to vote.
A window service that can copy and delete files across domains to keep 2 folders synchronized


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:

//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


  • 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.


  • 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.


  • 23rd April, 2009: Initial post


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


About the Author

Shaoying Wei
Web Developer
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionStarts then Stops Pin
kbassey10-Jun-09 5:19
memberkbassey10-Jun-09 5:19 
AnswerRe: Starts then Stops Pin
Shaoying Wei10-Jun-09 5:34
memberShaoying Wei10-Jun-09 5:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180515.1 | Last Updated 23 Apr 2009
Article Copyright 2009 by Shaoying Wei
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid