Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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:

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

Share

About the Author

Shaoying Wei
Web Developer
Canada Canada
No Biography provided

Comments and Discussions

 
QuestionIt works fine in Windows Server 2008 R2 and Windows 7 but not on Windows Server 2012 ... Pinmemberproxsys126-Jun-13 4:22 
QuestionInstallation error 2869 Pinmembersfsdfsfsdff5-Jan-12 23:02 
AnswerRe: Installation error 2869 PinmemberMember 107322447-Apr-14 20:35 
GeneralMail notification option Pinmemberarlosse4-May-11 4:07 
GeneralMy vote of 1 PinmemberJohn Kenedy S.Kom5-Nov-10 21:37 
GeneralSynchronizing 2 Folders (Folder1 -> Folder2 AND Folder1 <- Folder2) Pinmemberf r i s c h26-Jul-10 3:36 
GeneralRe: Synchronizing 2 Folders (Folder1 -> Folder2 AND Folder1 <- Folder2) Pinmemberpc_nsteam1-Nov-11 7:37 
GeneralMapping Issue Pinmemberwwilson7715-Sep-09 5:39 
GeneralRe: Mapping Issue [modified] PinmemberLuka6-Feb-12 1:28 
QuestionStarts then Stops Pinmemberkbassey10-Jun-09 5:19 
AnswerRe: Starts then Stops PinmemberShaoying Wei10-Jun-09 5:34 
Generalgood idea and good approach PinmemberDonsw16-May-09 17:08 
GeneralIs this is work in same network PinmemberK_V_R7-May-09 6:09 
GeneralNot 100% reliable... Pinmemberaxelriet24-Apr-09 2:03 

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

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

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