Synchronizing files with the server

If you have chosen to store your files in the database in a Binary Large Object (BLOB) field, then file synchronization is effortless—binary data is handled just like any other field. No additional code needs to be written. (For SQL Server CE, this is the image data type, and for Oracle, this is the BLOB data type).

If you are storing files externally in the file system, you will need to write additional code to sync them to the server. The Microsoft Synchronization Services Framework does the bulk of the work for you. You can map a folder on the server to a folder on the mobile device and the Sync framework will do the rest.

Tip

External file synchronization in an Oracle setup

Oracle Mobile Server does not provide an equivalent facility, so if you are using an Oracle setup, you will most likely need to write your own file synchronization service (or use the Microsoft Synchronization Services Framework).

Let's take a look at how this can be done. The first thing you need to do is to download and install the full Microsoft Sync Framework package (which contains the Sync Services for Filesystems components) from this location:

http://www.microsoft.com/downloads/details.aspx?FamilyId=C88BA2D1-CEF3-4149-B301-9B056E7FB1E6&displaylang=en

After installing this component, you will then need to add references to the following libraries:

  • Microsoft.Synchronization.Files.dll
  • Microsoft.Synchronization.dll

In your class, you will also need to import both the Microsoft.Synchronization and the Microsoft.Synchronization.Files namespaces. Setting up and running the sync is relatively straightforward. The following code shows how this can be easily achieved.

using Microsoft.Synchronization;
using Microsoft.Synchronization.Files;
public void Synchronize()
{
string _sourceFolder = "\My Documents";
string _destFolder = "d:\TargetFolder";
// Create the source and destination providers
FileSyncProvider sourceProvider = new
FileSyncProvider(Guid.NewGuid(), _sourceFolder);
FileSyncProvider destProvider = new
FileSyncProvider(Guid.NewGuid(), _destFolder);
// We request the providers to detect any changes in the folder
sourceProvider.DetectChanges();
destProvider.DetectChanges();
// We do a bidirectional sync of files in both folders
SyncOrchestrator agent = new SyncOrchestrator();
agent.LocalProvider = _sourceProvider;
agent.RemoteProvider = _destProvider;
agent.Direction = SyncDirectionOrder.UploadAndDownload;
agent.Synchronize();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.133.96