码迷,mamicode.com
首页 > 其他好文 > 详细

Microsoft Sync Framework同步文件

时间:2016-03-01 18:35:30      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

 1 public partial class Form1 : Form
 2     {
 3         private SyncOrchestrator sync = new SyncOrchestrator();
 4         private FileSyncProvider sourceProvider, targetProvider;
 5         private SyncDirectionOrder syncDirectionOrder = SyncDirectionOrder.Upload;
 6         private string sourcePath;
 7         private string targetPath;
 8         public Form1()
 9         {
10             InitializeComponent();
11             sync.StateChanged += (sender, args) =>
12             {
13                 this.labStatus.Text = sync.State.ToString();
14             };
15         }
16 
17         private void button1_Click(object sender, EventArgs e)
18         {
19             this.btnStartOrEnd.Text = this.btnStartOrEnd.Text == "开启" ? "关闭" : "开启";
20             sourcePath = this.txtSource.Text.Trim();
21             targetPath = this.txtTarget.Text.Trim();
22             
23             if (this.rbtnDownload.Checked)
24             {
25                 syncDirectionOrder = SyncDirectionOrder.Download;
26             }
27             if (this.rbtnUpload.Checked)
28             {
29                 syncDirectionOrder = SyncDirectionOrder.Upload;
30             }
31             if (this.rbtnDownloadAndUpload.Checked)
32             {
33                 syncDirectionOrder = SyncDirectionOrder.DownloadAndUpload;
34             }
35             if (this.rbtnUploadAndDownload.Checked)
36             {
37                 syncDirectionOrder = SyncDirectionOrder.UploadAndDownload;
38             }
39             if (this.btnStartOrEnd.Text == "关闭")
40             {
41                 var thread = new Thread(Start) { IsBackground = true };
42                 thread.Start();
43             }
44             else
45             {
46                 sync.Cancel();
47             }
48 
49         }
50 
51         private void Start()
52         {
53             while (true)
54             {
55                 sourceProvider = new FileSyncProvider(Guid.NewGuid(), sourcePath);
56                 targetProvider = new FileSyncProvider(Guid.NewGuid(), targetPath);
57                 sync = new SyncOrchestrator
58                 {
59                     LocalProvider = sourceProvider,
60                     RemoteProvider = targetProvider,
61                     Direction = syncDirectionOrder
62                 };
63                 sync.Synchronize();
64                 Thread.Sleep(100);
65             }
66         }
67     }

PS:要先装Microsoft Sync Framework SDK

Microsoft Sync Framework同步文件

标签:

原文地址:http://www.cnblogs.com/Leawee/p/5231890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!