Click here to Skip to main content
15,868,164 members
Articles / Desktop Programming / Win32

Real Time Folder Synchronization Window Service

Rate me:
Please Sign up or sign in to vote.
3.92/5 (8 votes)
23 Apr 2009CPOL2 min read 123.7K   9.4K   65   25
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)


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInstaller Broken Pin
Member 1316295022-Oct-19 4:38
Member 1316295022-Oct-19 4:38 
SuggestionDownloads no longer work Pin
citizendc27-Jun-19 6:00
citizendc27-Jun-19 6:00 
GeneralRe: Downloads no longer work Pin
OriginalGriff27-Jun-19 6:04
mveOriginalGriff27-Jun-19 6:04 
GeneralRe: Downloads no longer work Pin
citizendc27-Jun-19 6:34
citizendc27-Jun-19 6:34 
GeneralRe: Downloads no longer work Pin
OriginalGriff27-Jun-19 6:36
mveOriginalGriff27-Jun-19 6:36 
QuestionCould this work if you needed to map to Sharepoint Pin
Martin Mato5-Jun-17 23:08
Martin Mato5-Jun-17 23:08 
SuggestionA sotware best suited for the job. Pin
Member 1300532216-Feb-17 3:03
Member 1300532216-Feb-17 3:03 
QuestionFilesystemwatcher Pin
Member 1138592920-Jan-15 2:35
Member 1138592920-Jan-15 2:35 
QuestionSync 3 Folder Pin
Member 1131377115-Dec-14 20:45
Member 1131377115-Dec-14 20:45 
BugDoesn't Work Pin
Member 1105488725-Sep-14 4:10
professionalMember 1105488725-Sep-14 4:10 
GeneralRe: Doesn't Work Pin
Member 1131377115-Dec-14 20:45
Member 1131377115-Dec-14 20:45 
QuestionIt works fine in Windows Server 2008 R2 and Windows 7 but not on Windows Server 2012 ... Pin
proxsys126-Jun-13 4:22
proxsys126-Jun-13 4:22 
QuestionInstallation error 2869 Pin
bhavin1055-Jan-12 23:02
bhavin1055-Jan-12 23:02 
AnswerRe: Installation error 2869 Pin
Member 107322447-Apr-14 20:35
Member 107322447-Apr-14 20:35 
GeneralMail notification option Pin
arlosse4-May-11 4:07
arlosse4-May-11 4:07 
GeneralMy vote of 1 Pin
John Kenedy S.Kom5-Nov-10 21:37
John Kenedy S.Kom5-Nov-10 21:37 
GeneralSynchronizing 2 Folders (Folder1 -> Folder2 AND Folder1 <- Folder2) Pin
f r i s c h26-Jul-10 3:36
f r i s c h26-Jul-10 3:36 
GeneralRe: Synchronizing 2 Folders (Folder1 -> Folder2 AND Folder1 <- Folder2) Pin
NearSolutions1-Nov-11 7:37
NearSolutions1-Nov-11 7:37 
GeneralMapping Issue Pin
wwilson7715-Sep-09 5:39
wwilson7715-Sep-09 5:39 
GeneralRe: Mapping Issue Pin
Luka6-Feb-12 1:28
Luka6-Feb-12 1:28 
QuestionStarts then Stops Pin
kbassey10-Jun-09 5:19
kbassey10-Jun-09 5:19 
AnswerRe: Starts then Stops Pin
Shaoying Wei10-Jun-09 5:34
Shaoying Wei10-Jun-09 5:34 
Generalgood idea and good approach Pin
Donsw16-May-09 17:08
Donsw16-May-09 17:08 
GeneralIs this is work in same network Pin
K_V_R7-May-09 6:09
K_V_R7-May-09 6:09 
GeneralNot 100% reliable... Pin
Axel Rietschin24-Apr-09 2:03
professionalAxel Rietschin24-Apr-09 2:03 

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.