【问题标题】:Can not sync files on network while using Microsoft Sync Framework on two different machines在两台不同的机器上使用 Microsoft Sync Framework 时无法同步网络上的文件
【发布时间】:2020-08-14 13:58:20
【问题描述】:

我正在尝试在网络上使用 MS 同步框架。但它给了我一个例外找不到路径的一部分。谁能告诉我如何在网络上建立连接,以便通过网络同步不同计算机上的两个文件夹。 我可以从我的资源管理器中访问该文件夹,因此共享或权限没有问题。我在同一个wifi网络中。我有一个线索,也许我必须以某种方式输入凭据。

 private void button1_Click(object sender, EventArgs e)
        {
     
           FileSyncProvider sourceReplica = new FileSyncProvider(Guid.NewGuid(), @"E:\test");
            FileSyncProvider destReplica = new FileSyncProvider(Guid.NewGuid(), @"\\192.168.1.9\New Folder"); //Here I am getting exception.
  
            SyncOrchestrator agent = new SyncOrchestrator();
            agent.LocalProvider = sourceReplica;
            agent.RemoteProvider = destReplica;
            agent.Direction = SyncDirectionOrder.UploadAndDownload;
            agent.Synchronize();

        }

【问题讨论】:

    标签: c# microsoft-sync-framework


    【解决方案1】:

    所以在花了很多时间之后,我终于找到了一个解决方案。实际上问题出在凭据上。所以我在网上研究并找到了解决方案。

    我从类似这样的答案中复制了一个文件。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace syncDemo
    {
        class ConnectToSharedFolder :IDisposable
        {
            readonly string _networkName;
    
            public ConnectToSharedFolder(string networkName, NetworkCredential credentials)
            {
                _networkName = networkName;
    
                var netResource = new NetResource
                {
                    Scope = ResourceScope.GlobalNetwork,
                    ResourceType = ResourceType.Disk,
                    DisplayType = ResourceDisplaytype.Share,
                    RemoteName = networkName
                };
    
                var userName = string.IsNullOrEmpty(credentials.Domain)
                    ? credentials.UserName
                    : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
    
                var result = WNetAddConnection2(
                    netResource,
                    credentials.Password,
                    userName,
                    0);
    
                if (result != 0)
                {
                    throw new Win32Exception(result, "Error connecting to remote share");
                }
            }
    
            ~ConnectToSharedFolder()
            {
                Dispose(false);
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
    
            protected virtual void Dispose(bool disposing)
            {
                WNetCancelConnection2(_networkName, 0, true);
            }
    
            [DllImport("mpr.dll")]
            private static extern int WNetAddConnection2(NetResource netResource,
                string password, string username, int flags);
    
            [DllImport("mpr.dll")]
            private static extern int WNetCancelConnection2(string name, int flags,
                bool force);
    
            [StructLayout(LayoutKind.Sequential)]
            public class NetResource
            {
                public ResourceScope Scope;
                public ResourceType ResourceType;
                public ResourceDisplaytype DisplayType;
                public int Usage;
                public string LocalName;
                public string RemoteName;
                public string Comment;
                public string Provider;
            }
    
            public enum ResourceScope : int
            {
                Connected = 1,
                GlobalNetwork,
                Remembered,
                Recent,
                Context
            };
    
            public enum ResourceType : int
            {
                Any = 0,
                Disk = 1,
                Print = 2,
                Reserved = 8,
            }
    
            public enum ResourceDisplaytype : int
            {
                Generic = 0x0,
                Domain = 0x01,
                Server = 0x02,
                Share = 0x03,
                File = 0x04,
                Group = 0x05,
                Network = 0x06,
                Root = 0x07,
                Shareadmin = 0x08,
                Directory = 0x09,
                Tree = 0x0a,
                Ndscontainer = 0x0b
            }
        }
    }
    

    在我的主要形式中,我这样做了。

    public string networkPath = @"\\{I.P}\New Folder";
            NetworkCredential credentials = new NetworkCredential(@"userName", "password");
    
    private void button1_Click(object sender, EventArgs e)
            {
    
                FileSyncProvider sourceReplica = new FileSyncProvider(Guid.NewGuid(), @"E:\test");
                FileSyncProvider destReplica;
                using (new ConnectToSharedFolder(networkPath, credentials))
                {
                   destReplica = new FileSyncProvider(Guid.NewGuid(), @"\\192.168.1.9\New Folder");
                }
                
                SyncOrchestrator agent = new SyncOrchestrator();
                agent.LocalProvider = sourceReplica;
                agent.RemoteProvider = destReplica;
                agent.Direction = SyncDirectionOrder.UploadAndDownload;
                agent.Synchronize();
    
            }
    
    

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多