Use the Sync Framework in Windows Mobile

No.of Views1892
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:46
Tag : Windows Mobile , How to
The Sync Framework is powerful and easy to user in windows mobile to sync the data between to end points.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Microsoft introduced new framework for all type sync. This framework have variety type of the specify sync operation with database or filesystem.
List of those are,

1.ADO.Net
2.Common(File System)


Now you can downlaod SyncFramework from bellow url
http://www.microsoft.com/downloads/details.aspx?FamilyId=35E8F16E-AAA4-4919-8B3C-1CE4EA1F6552&displaylang=en


The Sync framework have managed class libraries. So we can code with C# or other your desired language (MS).After download and installed , create a new Project with C# or your desired language. I had create console application with C# After you must add References Microsoft Synchronization.

CodeSnippet

using System;using System.IO;using Microsoft.Synchronization;using Microsoft.Synchronization.Files;public class FileSyncProviderSample
{public static void Main( string [] args){if (args.Length
string .IsNullOrEmpty(args[0]) || string .IsNullOrEmpty(args[1]) ||
! Directory .Exists(args[0]) || ! Directory .Exists(args[1])){Console .WriteLine("Usage: FileSyncSample [valid directory path 1] [valid directory path 2]" );return ;}string replica1RootPath = args[0];string replica2RootPath = args[1];string idFileName = "filesync.id" ;SyncId replica1Id = GetReplicaId( Path .Combine(args[0], idFileName));SyncId replica2Id = GetReplicaId( Path .Combine(args[1], idFileName));try{// Set options for the sync operationFileSyncOptions options = FileSyncOptions.ExplicitDetectChanges |
FileSyncOptions.RecycleDeletes | FileSyncOptions.RecycleOverwrites;FileSyncScopeFilter filter = new FileSyncScopeFilter();filter.FileNameExcludes.Add(idFileName); // Exclude the id file// Explicitly detect changes on both replicas upfront, to avoid two change// detection passes for the two-way syncDetectChangesOnFileSystemReplica(replica1Id, replica1RootPath, filter, options);DetectChangesOnFileSystemReplica(replica2Id, replica2RootPath, filter, options);// Sync in both directionsSyncFileSystemReplicasOneWay(replica1Id, replica2Id,replica1RootPath, replica2RootPath, filter, options);SyncFileSystemReplicasOneWay(replica2Id, replica1Id,replica2RootPath, replica1RootPath, filter, options);}catch ( Exception e){Console .WriteLine( "\nException from File System Provider:\n" + e.ToString());}}public static void DetectChangesOnFileSystemReplica(SyncId replicaId, string replicaRootPath,FileSyncScopeFilter filter, FileSyncOptions options){FileSyncProvider provider = null ;try{provider = new FileSyncProvider(replicaId, replicaRootPath, filter, options);provider.DetectChanges();}finally{// Release resourcesif (provider != null )provider.Dispose();}}public static void SyncFileSystemReplicasOneWay(SyncId sourceReplicaId, SyncId destinationReplicaId,string sourceReplicaRootPath, string destinationReplicaRootPath,FileSyncScopeFilter filter, FileSyncOptions options){FileSyncProvider sourceProvider = null ;FileSyncProvider destinationProvider = null ;try{sourceProvider = new FileSyncProvider(sourceReplicaId, sourceReplicaRootPath, filter, options);destinationProvider = new FileSyncProvider(destinationReplicaId, destinationReplicaRootPath, filter, options);destinationProvider.AppliedChange +=new EventHandler (OnAppliedChange);destinationProvider.SkippedChange +=new EventHandler (OnSkippedChange);SyncAgent agent = new SyncAgent();agent.LocalProvider = sourceProvider;agent.RemoteProvider = destinationProvider;agent.Direction = SyncDirection.Upload; // Sync source to destinationConsole .WriteLine( "Synchronizing changes to replica: " +destinationProvider.RootDirectoryPath);agent.Synchronize();}finally{// Release resourcesif (sourceProvider != null ) sourceProvider.Dispose();if (destinationProvider != null ) destinationProvider.Dispose();}}public static void OnAppliedChange( object sender, AppliedChangeEventArgs args){switch (args.ChangeType){case ChangeType.Create:Console .WriteLine( "-- Applied CREATE for file " + args.NewFilePath);break ;case ChangeType.Delete:Console .WriteLine( "-- Applied DELETE for file " + args.OldFilePath);break ;case ChangeType.Overwrite:Console .WriteLine( "-- Applied OVERWRITE for file " + args.OldFilePath);break ;case ChangeType.Rename:Console .WriteLine( "-- Applied RENAME for file " + args.OldFilePath +" as " + args.NewFilePath);break ;}}public static void OnSkippedChange( object sender, SkippedChangeEventArgs args){Console .WriteLine( "-- Skipped applying " + args.ChangeType.ToString().ToUpper()+ " for " + (! string .IsNullOrEmpty(args.CurrentFilePath) ?
args.CurrentFilePath : args.NewFilePath) + " due to error" );if (args.Exception != null )Console .WriteLine( " [" + args.Exception.Message + "]" );}public static SyncId GetReplicaId( string idFilePath){SyncId replicaId = null ;if ( File .Exists(idFilePath)){using ( StreamReader sr = File .OpenText(idFilePath)){string strGuid = sr.ReadLine();if (! string .IsNullOrEmpty(strGuid))replicaId = new SyncId( new Guid (strGuid));}}if (replicaId == null ){using ( FileStream idFile = File .Open(idFilePath, FileMode .OpenOrCreate, FileAccess .ReadWrite)){using ( StreamWriter sw = new StreamWriter (idFile)){replicaId = new SyncId( Guid .NewGuid());sw.WriteLine(replicaId.GetGuidId().ToString( "D" ));}}}return replicaId;}}

basically sync framework work GUID as references. Because of Sync is time depend operation,so we must need a unique id for identification our file/DB is changed , removed and etc.

Conclusion

The SyncFramework has great feature for sync operation to all types of operation. You can use the Sync framework for Database Sync, File System Sync and Mobile device File Sync

References

http://msdn2.microsoft.com/en-us/sync/default.aspx 

http://msdn2.microsoft.com/en-us/sync/bb887623.aspx

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:RRaveenDate Of Posted:5/30/2010 9:55:12 PM
Yes
Yes, There few type of Sync providers,ADO.NET CE is give to support to sync between to .NET CF and .NET Framework application data. its mean say you have main sql server and sql ce you able to sync between do easily. If you want to use the File System then you able to do the custom coding.but have support. if you have any more question, please post in message board codegain experts will try to give answers. thank you
By:CarnDate Of Posted:5/30/2010 9:06:51 PM
Can be used in mobile devices?
Hi There, I am just wondering is this file sync provider able to be used in mobile ? I mean like running in mobile environment. Coz from wat i found out from the net saying that sync framework only able to run in Desktop. Correct me if I m wrong. Also, if it is running at desktop, for the file path i put \ as the device directory, it appears to me an error showing method missing exception, or the path couldnt be found. For desktop running syncing both folder with the same code, no errors appeared. Do you know any solution of this? Thanks
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top