locked
Microsoft Synchronization Framework 2.1 not working in Windows 10 Visual Studio 2015 Administrator mode RRS feed

  • Question

  • My Sync code is working well in Wind 8 Visual Studio 2015 Admin mode, while the same code is not working in Wind 10 Visual Studio 2015 administrator mode.

    using Microsoft.Synchronization;
    using Microsoft.Synchronization.Files;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace WpfApplication6
    {
        public class myGlobal
        {
            private string str;
            public string SetValue
            {
                get { return str; }
                set { str = value; }
            }
        }
        
        public class Copy
        {
            public static myGlobal strvalue;
            string source, destination;
            public Copy(string sourcepath,string destinationpath)
            {
                strvalue = new myGlobal();
                source = sourcepath;
                destination = destinationpath;
                CreateSync();
            }

            private void CreateSync()
            {
                string replica1RootPath = destination;
                string despath = replica1RootPath + @"\Database"+DateTime.Now.ToString("yyyymmddhhMMss");
                //int offset = source.IndexOf(@"\");
                //int firstoffset = source.IndexOf(@"\", offset + 1);
                //int lastoffset = source.IndexOf(@"\", firstoffset + 1);
                ////string hospitalname = source.Substring(9, 19);
                //string hospitaldirectory = path + source.Substring(firstoffset, lastoffset - firstoffset);
                //int offsetafter = source.IndexOf(@"\", lastoffset + 1);
                //string datedirectory = hospitaldirectory + source.Substring(lastoffset, offsetafter - lastoffset);
                //string foldernamedirectory = datedirectory + source.Substring(offsetafter);

                if(Directory.Exists(destination))
                if (Directory.Exists(replica1RootPath))
                {
                    Directory.CreateDirectory(despath);
                    
                }
                else
                {
                        CreateLogFile("Invalid Destination path!");
                }
                string replica2RootPath = source;

                try
                {
                    // Set options for the synchronization operation
                    FileSyncOptions options = FileSyncOptions.RecycleDeletedFiles;

                    FileSyncScopeFilter filter = new FileSyncScopeFilter();
                    filter.FileNameExcludes.Add("*.lnk"); // Exclude all *.lnk files

                    // Explicitly detect changes on both replicas upfront, to avoid two change
                    // detection passes for the two-way synchronization

                    // DetectChangesOnFileSystemReplica(foldernamedirectory, filter, options);
                    DetectChangesOnFileSystemReplica(replica2RootPath, filter, options);

                    // Synchronization in both directions
                    //   SyncFileSystemReplicasOneWay(replica1RootPath, replica2RootPath, null, options);
                    SyncFileSystemReplicasOneWay(replica2RootPath, despath, null, options);
                }
                catch (Exception ex)
                {
                    CreateLogFile(ex.ToString());
                   // MessageBox.Show("\nException from File Synchronization Provider:\n" + ex.ToString());
                    //Console.Read();
                }

            }
            public static void OnAppliedChange(object sender, AppliedChangeEventArgs args)
            {

                switch (args.ChangeType)
                {
                    case ChangeType.Create:
                        strvalue.SetValue = "Applied CREATE for file " + args.NewFilePath;

                        //Console.Read();
                        break;
                    case ChangeType.Delete:
                        strvalue.SetValue = "-- Applied DELETE for file " + args.OldFilePath;
                        //Console.Read();
                        break;
                    //case ChangeType.Overwrite:
                    //    Console.WriteLine("-- Applied OVERWRITE for file " + args.OldFilePath);
                    //    break;
                    case ChangeType.Rename:
                        strvalue.SetValue = "-- Applied RENAME for file " + args.OldFilePath +
                                          " as " + args.NewFilePath;
                        
                        //  Console.Read();
                        break;

                }
                // te.Text = str;
            }
       
            public void CreateLogFile(string text)
            {
                string path1 = System.IO.Path.GetDirectoryName(
          System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\Database";
                path1 = path1.Remove(0, 6);
                string textfile = path1 + @"\Log.txt";
                if ((!System.IO.Directory.Exists(path1)))
                {
                    System.IO.Directory.CreateDirectory(path1);
                }
                if (System.IO.File.Exists(textfile))
                {
                    File.Delete(textfile);
                }

                int counter = 0;
                Lable:
                counter++;
                try
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(textfile, true))
                    {
                        file.WriteLine(text);
                        if(strvalue!=null)
                            file.WriteLine(strvalue.ToString());
                    }
                }
                catch
                {
                    Process proc = Process.Start(textfile);
                    // do stuff
                    proc.Kill();
                    if(counter<4)
                    goto Lable;
                }
            }
            public static void SyncFileSystemReplicasOneWay(string sourceReplicaRootPath, string destinationReplicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options)
            {
                Microsoft.Synchronization.Files.FileSyncProvider sourceProvider = null;
                FileSyncProvider destinationProvider = null;

                try
                {

                    Guid gid, gidd;
                    // gid = System.InteropServices;
                    gid = Guid.NewGuid();
                    gidd = Guid.NewGuid();
                    sourceProvider = new Microsoft.Synchronization.Files.FileSyncProvider(gid, sourceReplicaRootPath, filter, options);
                    destinationProvider = new Microsoft.Synchronization.Files.FileSyncProvider(gidd, destinationReplicaRootPath, filter, options);

                    destinationProvider.AppliedChange +=
                        new EventHandler<AppliedChangeEventArgs>(OnAppliedChange);
                    destinationProvider.SkippedChange +=
                        new EventHandler<SkippedChangeEventArgs>(OnSkippedChange);

                    SyncOrchestrator agent = new SyncOrchestrator();
                    agent.LocalProvider = sourceProvider;
                    agent.RemoteProvider = destinationProvider;
                    agent.Direction = SyncDirectionOrder.Upload; // Synchronize source to destination

                    // Console.WriteLine("Synchronizing changes to replica: " +
                    //   destinationProvider.RootDirectoryPath);
                    agent.Synchronize();
                    //Console.Read();
                }
                finally
                {
                    // Release resources
                    if (sourceProvider != null) sourceProvider.Dispose();
                    if (destinationProvider != null) destinationProvider.Dispose();
                }
            }
            public static void OnSkippedChange(object sender, SkippedChangeEventArgs args)
            {
                strvalue.SetValue="-- Skipped applying " + args.ChangeType.ToString().ToUpper()
                      + " for " + (!string.IsNullOrEmpty(args.CurrentFilePath) ?
                                    args.CurrentFilePath : args.NewFilePath) + " due to error";

                if (args.Exception != null)
                    strvalue.SetValue = "   [" + args.Exception.Message + "]";
                
            }

            public static void DetectChangesOnFileSystemReplica(string replicaRootPath, FileSyncScopeFilter filter, FileSyncOptions options)
            {
                FileSyncProvider provider = null;
                Guid gid = Guid.NewGuid();
                try
                {
                    provider = new FileSyncProvider(gid, replicaRootPath, filter, options);
                    provider.DetectChanges();
                }
                finally
                {
                    // Release resources
                    if (provider != null)
                        provider.Dispose();
                }
            }









        }
    }


    • Edited by Xmancoder Tuesday, February 23, 2016 9:47 AM
    • Moved by Kristin Xie Wednesday, February 24, 2016 2:53 AM Microsoft Sync Framework related
    Tuesday, February 23, 2016 9:43 AM

All replies

  • Hi Nixaam,

    >>My Sync code is working well in Wind 8 Visual Studio 2015 Admin mode, while the same code is not working in Wind 10 Visual Studio 2015 administrator mode.

    This forum is about the .net Framework C# programming language, IDE, libraries, samples, and tools. As your issue is more related to the Microsoft Synchronization Framework, we would help you move this thread to the SyncFx - Microsoft Sync Framework Developer Discussions forum for better support.

    Besides, as the above code just shows the definitions of two classes, you’d better provide more things to show how you call them and what error you encountered when you run the code in Windows 10 Visual Studio 2015 administrator mode, so that we could know your issue clearly and better help you.

    Thank you for your understanding.

    Best Regards,

    Albert Zhang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 24, 2016 2:50 AM