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.
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 program here demonstrates how to addresses the above issues:
This class is used to observe the source folder and raises event whenever any change is detected in the folder:
FileSystemWatcher _watchFolder = new FileSystemWatcher();
string srcRoot = ConfigurationManager.AppSettings["localFolder"];
_watchFolder.Path = srcRoot;
_watchFolder.IncludeSubdirectories = true;
_watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
_watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
_watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;
_watchFolder.Changed += new FileSystemEventHandler(eventChangeRaised);
_watchFolder.Created += new FileSystemEventHandler(eventCreateRaised);
_watchFolder.Deleted += new FileSystemEventHandler(eventDeleteRaised);
_watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRenameRaised);
_watchFolder.EnableRaisingEvents = true;
The program will run whenever the computer is up.
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.
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