首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在两台不同的计算机上使用Microsoft sync Framework时,无法同步网络上的文件

在两台不同的计算机上使用Microsoft sync Framework时,无法同步网络上的文件
EN

Stack Overflow用户
提问于 2020-08-14 21:58:20
回答 1查看 74关注 0票数 0

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

代码语言:javascript
运行
复制
 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();

        }
EN

回答 1

Stack Overflow用户

发布于 2020-08-15 16:02:22

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

我从一个答案中复制了一个文件,如下所示。

代码语言:javascript
运行
复制
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
        }
    }
}

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

代码语言:javascript
运行
复制
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();

        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63414217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档