Microsoft Sync Framework同步文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Microsoft Sync Framework同步文件相关的知识,希望对你有一定的参考价值。
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同步文件的主要内容,如果未能解决你的问题,请参考以下文章
Microsoft Sync Framework 2.1 的状态如何
Microsoft Sync Framework 错误删除外键链接表
Microsoft Sync Framework 1.0 RTM发布了