locked
Copy within applyingChangeEvent triggers applyingChangeEvent RRS feed

  • Question

  • Dear readers,

    What I'm trying to accomplish is copying a file before it is overwritten or deleted. But whenever I try to do this the System.IO.File.Copy command triggers an applyingChangeEvent. For example: when I want to Upload the file C:\temp\Readme.txt to D:\temp\ and in D:\temp an older file named Readme.txt exists; I want to backup the file D:\temp\Readme.txt to D:\Backup\ before overwriting D:\temp\Readme.txt with the version of C:\temp\Readme.txt. The location D:\Backup\ is outside the fileSyncProvider's rootDirectoryPaths. Whenever I use the command System.IO.File.Copy("D:\temp\readme.txt", "D:\Backup\readme.txt") in the applyingChangeEvent (ChangeType.Update) a new applyingChangeEvent is triggered for Readme.txt (now with ChangeType.Create) and afterwards this Create process reports that D:\temp\Readme.txt already exists.

    Can anyone help me?

    Below the code (please don't crusifix me for the code). 

      private void ApplyingChange(object sender, MSF.ApplyingChangeEventArgs applyingChangeEventArgs)
    
      {
    
       switch (applyingChangeEventArgs.ChangeType)
    
       {
    
        case MSF.ChangeType.Create:
    
         LogText("-- Applying CREATE for file " + applyingChangeEventArgs.NewFileData.RelativePath);
    
         break;
    
        case MSF.ChangeType.Delete:
    
         if (this.DeleteAndOverwriteMode == DeleteAndOverwriteMode.NoDelete)
    
         {
    
          LogText("-- Ignoring DELETE for file " + applyingChangeEventArgs.CurrentFileData.RelativePath);
    
          applyingChangeEventArgs.SkipChange = true;
    
          break;
    
         }
    
         if (this.DeleteAndOverwriteMode == DeleteAndOverwriteMode.MakeBackup)
    
         {
    
          if (applyingChangeEventArgs.CurrentFileData.IsDirectory == false)
    
          {
    
           MSF.FileSyncProvider fileSyncProvider = ((MSF.FileSyncProvider)sender);
    
    
    
           LogText("-- Making BACKUP for file " + applyingChangeEventArgs.CurrentFileData.RelativePath);
    
    
    
           string _sourcePathCopy =
    
            fileSyncProvider.RootDirectoryPath.TrimEnd('\\') + 
    
            "\\" + applyingChangeEventArgs.CurrentFileData.RelativePath;
    
    
    
           string _targetPathCopy = this.PathBackup.TrimEnd('\\') + "\\LiQuickSyncToy_Backup\\" +
    
            _DateTime.Year.ToString().Trim() +
    
            _DateTime.Month.ToString().Trim().PadLeft(2, '0') +
    
            _DateTime.Day.ToString().Trim().PadLeft(2, '0') + "_" +
    
            _DateTime.Hour.ToString().Trim().PadLeft(2, '0') +
    
            _DateTime.Minute.ToString().Trim().PadLeft(2, '0') +
    
            "\\" + applyingChangeEventArgs.CurrentFileData.RelativePath;
    
    
    
           
    
           System.IO.Directory.CreateDirectory(
    
            _targetPathCopy.Substring(0, _targetPathCopy.Length - applyingChangeEventArgs.CurrentFileData.Name.Length));
    
           System.IO.File.Copy(
    
            _sourcePathCopy, _targetPathCopy);
    
          }
    
         }
    
         LogText("-- Applying DELETE for file " + applyingChangeEventArgs.CurrentFileData.RelativePath);
    
         break;
    
        case MSF.ChangeType.Update:
    
         if (this.DeleteAndOverwriteMode == DeleteAndOverwriteMode.MakeBackup)
    
         {
    
          MSF.FileSyncProvider fileSyncProvider = ((MSF.FileSyncProvider)sender);
    
    
    
          LogText("-- Making BACKUP for file " + applyingChangeEventArgs.CurrentFileData.RelativePath);
    
    
    
          string _sourcePathCopy =
    
           fileSyncProvider.RootDirectoryPath.TrimEnd('\\') +
    
           "\\" + applyingChangeEventArgs.CurrentFileData.RelativePath;
    
    
    
          string _targetPathCopy = this.PathBackup.TrimEnd('\\') + "\\LiQuickSyncToy_Backup\\" +
    
           _DateTime.Year.ToString().Trim() +
    
           _DateTime.Month.ToString().Trim().PadLeft(2, '0') +
    
           _DateTime.Day.ToString().Trim().PadLeft(2, '0') + "_" +
    
           _DateTime.Hour.ToString().Trim().PadLeft(2, '0') +
    
           _DateTime.Minute.ToString().Trim().PadLeft(2, '0') +
    
           "\\" + applyingChangeEventArgs.CurrentFileData.RelativePath;
    
    
    
          System.IO.Directory.CreateDirectory(System.IO.Directory.GetDirectoryRoot(_targetPathCopy));
    
          System.IO.File.Copy(
    
           _sourcePathCopy, _targetPathCopy);
    
         }
    
         LogText("-- Applying OVERWRITE for file " + applyingChangeEventArgs.CurrentFileData.RelativePath);
    
         break;
    
        case MSF.ChangeType.Rename:
    
         LogText("-- Applying RENAME for file " + applyingChangeEventArgs.CurrentFileData.RelativePath +
    
          " as " + applyingChangeEventArgs.NewFileData.RelativePath);
    
         break;
    
       }
    
      }
    
    
    
    

     

    • Edited by LiQuick Tuesday, September 28, 2010 12:55 PM Clarification
    Tuesday, September 28, 2010 12:52 PM

All replies

  • Hi LiQuick,

    I apologize for the late reply. From your code, I can't seem to guess why it would cause another applyingchanges call.

    The only case I can think of would be that maybe apply changes call failed (possibly due to existing file handle to the file to be updated)

    and that causes a retry on the same file. 

     

    Here is something I dug out from the MSDN:

    http://msdn.microsoft.com/en-us/library/microsoft.synchronization.files.filesyncprovider.applyingchange.aspx 

    Some changes are tried more than one time during the course of a session. On each attempt, this event is fired. For example, an attempt is made to delete a folder. Because there is an existing file in the folder, the attempt fails. This folder delete operation will continue to be tried throughout the session until either the delete operation is successful or it is determined that the delete operation cannot occur during this session and is skipped.

     

    Hope this helps,

     

    Patrick

    Wednesday, October 13, 2010 8:58 PM