locked
file synch one way RRS feed

  • Question

  • Hello


    i use the following code

    < Code >

    Imports

     

    System

    Imports

     

    System.IO

    Imports

     

    Microsoft.Synchronization

    Imports

     

    Microsoft.Synchronization.Files

    Public

     

    Class FileSynch

     

    '' Create a filter that excludes all *.lnk files. The same filter should be used

     

    '' by both providers.

     

    'Dim filter As New FileSyncScopeFilter()

     

    ' filter.FileNameExcludes.Add("*.lnk")

     

    Public Sub SynchronizeFiles(ByVal SourcePath As String, ByVal DestinationPath As String, ByVal filter As FileSyncScopeFilter)

     

    ' Set options for the synchronization session.

     

    Dim options As FileSyncOptions = FileSyncOptions.CompareFileStreams

    SynchronizeFiles(SourcePath, DestinationPath, options, filter)

     

    End Sub

     

    Public Sub SynchronizeFiles(ByVal SourcePath As String, ByVal DestinationPath As String, ByVal Options As FileSyncOptions, ByVal filter As FileSyncScopeFilter)

     

    ' Explicitly detect changes on both replicas before syncyhronization occurs.

     

    ' This avoids two change detection passes for the bidirectional synchronization

     

    ' that we will perform.

    DetectChangesOnFileSystemReplica(SourcePath, filter, Options)

     

    ' Synchronize the replicas .

    SyncFileSystemReplicasOneWay(SourcePath, DestinationPath, filter, Options)

     

    End Sub

     

    ' Create a provider, and detect changes on the replica that the provider

     

    ' represents.

     

    Public Sub DetectChangesOnFileSystemReplica(ByVal replicaRootPath As String, ByVal filter As FileSyncScopeFilter, _

     

    ByVal options As FileSyncOptions)

     

    Dim provider As FileSyncProvider = Nothing

     

    Try

    provider =

    New FileSyncProvider(replicaRootPath, filter, options)

    provider.DetectChanges()

     

    Finally

     

    ' Release resources.

     

    If provider IsNot Nothing Then

    provider.Dispose()

     

    End If

     

    End Try

     

    End Sub

     

    Public Sub SyncFileSystemReplicasOneWay(ByVal sourceReplicaRootPath As String, _

     

    ByVal destinationReplicaRootPath As String, ByVal filter As FileSyncScopeFilter, _

     

    ByVal options As FileSyncOptions)

     

    Dim sourceProvider As FileSyncProvider = Nothing

     

    Dim destinationProvider As FileSyncProvider = Nothing

     

    Try

     

    ' Instantiate source and destination providers, with a null filter (the filter

     

    ' was specified in DetectChangesOnFileSystemReplica()), and options for both.

    sourceProvider =

    New FileSyncProvider(sourceReplicaRootPath, filter, options)

    destinationProvider =

    New FileSyncProvider(destinationReplicaRootPath, filter, options)

     

    ' Register event handlers so that we can write information

     

    ' to the console.

     

    AddHandler destinationProvider.AppliedChange, AddressOf OnAppliedChange

     

    AddHandler destinationProvider.SkippedChange, AddressOf OnSkippedChange

     

    ' Use SyncCallbacks for conflicting items.

     

    Dim destinationCallbacks As SyncCallbacks = destinationProvider.DestinationCallbacks

     

    AddHandler destinationCallbacks.ItemConflicting, AddressOf OnItemConflicting

     

    AddHandler destinationCallbacks.ItemConstraint, AddressOf OnItemConstraint

     

    Dim agent As New SyncOrchestrator()

    agent.LocalProvider = sourceProvider

    agent.RemoteProvider = destinationProvider

    agent.Direction = SyncDirectionOrder.Upload

     

    ' Upload changes from the source to the destination.

    Console.WriteLine(

    "Synchronizing changes to replica: " & destinationProvider.RootDirectoryPath)

    agent.Synchronize()

     

    Finally

     

    ' Release resources.

     

    If sourceProvider IsNot Nothing Then

    sourceProvider.Dispose()

     

    End If

     

    If destinationProvider IsNot Nothing Then

    destinationProvider.Dispose()

     

    End If

     

    End Try

     

    End Sub

     

    ' Provide information about files that were affected by the synchronization session.

     

    Public Event eOnAppliedChange(ByVal sender As Object, ByVal args As AppliedChangeEventArgs)

     

    Public Sub OnAppliedChange(ByVal sender As Object, ByVal args As AppliedChangeEventArgs)

     

    RaiseEvent eOnAppliedChange(sender, args)

     

    Select Case args.ChangeType

     

    Case ChangeType.Create

    Console.WriteLine(

    "-- Applied CREATE for file " & args.NewFilePath)

     

    Exit Select

     

    Case ChangeType.Delete

    Console.WriteLine(

    "-- Applied DELETE for file " & args.OldFilePath)

     

    Exit Select

     

    Case ChangeType.Update

    Console.WriteLine(

    "-- Applied OVERWRITE for file " & args.OldFilePath)

     

    Exit Select

     

    Case ChangeType.Rename

    Console.WriteLine((

    "-- Applied RENAME for file " & args.OldFilePath & " as ") & args.NewFilePath)

     

    Exit Select

     

    End Select

     

    End Sub

     

    ' Provide error information for any changes that were skipped.

     

    Public Event eOnSkippedChange(ByVal sender As Object, ByVal args As SkippedChangeEventArgs)

     

    Public Sub OnSkippedChange(ByVal sender As Object, ByVal args As SkippedChangeEventArgs)

     

    RaiseEvent eOnSkippedChange(sender, args)

    Console.WriteLine((

    "-- Skipped applying " & args.ChangeType.ToString().ToUpper() & " for ") & (If(Not String.IsNullOrEmpty(args.CurrentFilePath), args.CurrentFilePath, args.NewFilePath)) & " due to error")

     

    If args.Exception IsNot Nothing Then

    Console.WriteLine(

    " [" & args.Exception.Message & "]")

     

    End If

     

    End Sub

     

    ' By default, conflicts are resolved in favor of the last writer. In this example,

     

    ' the change from the source in the first session (replica 1), will always

     

    ' win the conflict.

     

    Public Event eOnItemConflicting(ByVal sender As Object, ByVal args As ItemConflictingEventArgs)

     

    Public Sub OnItemConflicting(ByVal sender As Object, ByVal args As ItemConflictingEventArgs)

     

    RaiseEvent eOnItemConflicting(sender, args)

    args.SetResolutionAction(ConflictResolutionAction.SourceWins)

    Console.WriteLine(

    "-- Concurrency conflict detected for item " & args.DestinationChange.ItemId.ToString())

     

    End Sub

     

    Public Event eOnItemConstraint(ByVal sender As Object, ByVal args As ItemConstraintEventArgs)

     

    Public Sub OnItemConstraint(ByVal sender As Object, ByVal args As ItemConstraintEventArgs)

     

    RaiseEvent eOnItemConstraint(sender, args)

    args.SetResolutionAction(ConstraintConflictResolutionAction.SourceWins)

    Console.WriteLine(

    "-- Constraint conflict detected for item " & args.DestinationChange.ItemId.ToString())

     

    End Sub

    End

     

    Class



    </code>


    and i call it from a test app like this 

     

    Dim x As New ista.SynChronization.FileSynch

    Debug.WriteLine(

    Date.Now) ' "O:\Testprogs\Bin\ ' P:\Nepros\Bin\

    x.SynchronizeFiles(

    "O:\Testprogs\Bin\", "C:\Test\", Nothing)

    Debug.WriteLine(

    Date.Now)

    MsgBox(

    "ready ?")


    P and O are 2 different network drives one containing a production version and one containing a test version
    however in the test version there might be some newer , older, same  or missing files as in production and vice versa i want my test app to synchronize local to the C:\Test\
    directory with the exact contents of one of these 2 directories and would like to switch between one of these 2 server stores 

    Is this possible ? and if wich options should i then set ? as currently the files that do not exist in the other storage remain local and it looks like some local newer files are not overwritten by older files from the remote storage  . 

    regards

    Michel Posseth  


     
    MCP
    Thursday, January 14, 2010 3:09 PM

Answers

  • HI Michel -

    I am not sure that I understand your scenario correctly - but one statement that stands out is that you say local newer files are not overwritten by older files from the remote storage. We use timestamps to detect which is the newest created file and it is not possible for that file to be overwritten by an older file unless you update the timestamp on the older file to be something newer.

    Thanks
    Deepa
    Deepa ( Microsoft Sync Framework)
    Thursday, January 14, 2010 7:32 PM
    Answerer

All replies

  • HI Michel -

    I am not sure that I understand your scenario correctly - but one statement that stands out is that you say local newer files are not overwritten by older files from the remote storage. We use timestamps to detect which is the newest created file and it is not possible for that file to be overwritten by an older file unless you update the timestamp on the older file to be something newer.

    Thanks
    Deepa
    Deepa ( Microsoft Sync Framework)
    Thursday, January 14, 2010 7:32 PM
    Answerer
  • Hello Deepa ,


    Your answer explains a lot ,,,,


    My complete scenario is that i want to synchronize   .Net XCopy deployable production and test applications from a server to the client
    so user logs in to his box starts our custom system menu and chooses then if he wants to work with the production version or with a test version of the software
    the software is copied to the local location and then he can start his ( production or test )  work .

    Currently i work with a custom method that calcultes a CRC on every file if the remote version is different as the local  it will be copied to the local location regardless if it is newer or older  the remote location should be leading , however this  custom process is slow and i hoped i could use the Synch Framework to speed things up

    regards

    Michel
    MCP
    Friday, January 15, 2010 7:55 AM
  • I'm building a similar tool. I need something that synchronizes from a source location and pushes out to several destinations, prod/test servers. In my scenario, I never want changes to be replicated to my source, but I want all changes from source to happen on the target. Specifically, if I delete files from source I want those deletes to happen on the destination. Thus far I can synch out changes and new files without a problem, but if I delete something on the source, that delete doesn't seem to propagate to the target...

    I'm assuming this has something to do with my options and SyncDirectionOrder. I've only been playing with this for a couple of hours, but have a quick answer would be awesome.

    Sunday, February 14, 2010 2:08 AM