How to: Synchronize Files by Using Managed Code
The examples in this topic focus on the following Sync Framework types:
Understanding File Synchronization
Sync Framework implements a synchronization provider that can be used to synchronize files and subfolders that are contained in a folder on a file system.
This provider exposes several configurable settings to give a finer degree of control over exactly how synchronization occurs and which items are synchronized.
To synchronize files between two folders, an application completes the following basic steps:
Creates an FileSyncProvider object to represent each folder.
Passes the two providers to an SyncOrchestrator object, and specifies one as the source provider and the other as the destination provider.
Calls Synchronize to start the synchronization session.
For more information about synchronizing files, see Synchronizing Files.
Example
The example code in this section is from a console application that synchronizes two directories, including the subdirectories and files in those directories.
The example code shows the following tasks:
How to set synchronization options.
How to explicitly perform change detection for a replica复制品.
How to specify a filter that controls which items are included in synchronization.
How to handle conflicts that can occur during synchronization.
How to synchronize two replicas.
After showing these code examples, we include the complete code for the application so that you can build and run it.
Setting Synchronization Options
The FileSyncOptions object enables you to set several options for file synchronization, including how to detect changes and whether to delete items during synchronization or move them to the Recycle Bin.
The following code example sets four options, three of which are related to item deletes.
The option ExplicitDetectChanges means that Sync Framework will not perform change detection unless the application explicitly callsDetectChanges.
This is explained in the next section "Performing Change Detection".
Performing Change Detection
By default, Sync Framework performs change detection at both replicas whenever Synchronize is called.
Change detection enables Sync Framework to determine which items should be sent from the source to the destination and which items, if any, are in conflict.
By specifyingExplicitDetectChanges, you can control when change detection is performed.
The following code example calls change detection for each replica before Synchronize is ever called.
This example is meant to illustrate DetectChanges, but it does have the benefit of having one change detection pass rather than the two that would occur when we perform bidirectional synchronization later in the application.
Specifying a Static Filter
Static filters can be set to exclude files by name (including wildcard通配符 names) and by attribute属性.
Static filters can also be set to exclude the contents of whole subfolders.
Or, an explicit list of file names to include (including wildcard names) can be specified.
To be included in the scope, a file or a folder must pass all filters.
For example, if all files that have a .txt extension are excluded from the scope and MyFile.txt is specified in the list of files to explicitly include in the scope, MyFile.txt will be excluded because of its .txt extension.
The following code example uses the FileSyncScopeFilter object to create a filter that excludes all *.lnk files.
A filter has no relationship to its creating provider.
To connect a filter to a provider, pass the filter to one of the constructors for FileSyncProvider or by setting the ScopeFilterproperty.
In the sample application, we do this in the DetectChangesOnFileSystemReplica() method because the filter is relevant only for change detection.
Because the filter is independent of the provider, only one filter should be created per synchronization session;
providers should not use different filters, because this can lead to non-convergence of data.
In addition to static filters, you can also exclude files during synchronization by handling an event raised by the provider.
For more information, see Controlling Which Files Are Synchronized.
Handling Conflicts
Sync Framework detects and resolves决定 concurrency并发 conflicts and constraint约束 conflicts for files and folders.
A concurrency conflict occurs when the same item is changed at both replicas since the last synchronization session between those replicas.
A constraint conflict occurs if a file or folder with the same name is added to both replicas.
Conflicts are resolved by keeping the file or folder with the most recent change and deleting (or moving) the file or folder with the older change.
For files, you also have the option of specifying that the source or destination should win the conflict, regardless of which change occurred first.
The following code example registers event handlers for the ItemConflicting and ItemConstraintevents that are available through the SyncCallbacks object.
The methods that are called resolve all conflicts in favor of有利于 the source and write information to the console.
Synchronizing Two Replicas
After options and filters are set, the application synchronizes the two replicas by instantiating a SyncOrchestrator, and calling the Synchronizemethod.
The following code example specifies the provider for each replica, sets options, registers event handlers, specifies a synchronization direction of Upload, and calls Synchronize.
The method is called twice to perform bidirectional synchronization between the replicas.
Complete Code Example
The following code is the complete code for this example. The previous examples in this section were taken from this code.
To run this code:
Create a console application project, and add the code to the project.
Add references to Microsoft.Synchronzation.dll and Microsoft.Synchronzation.Files.dll.
Build the project to create an executable.
Run the executable from the command line to synchronize the files and subdirectories of two replica directories: MyExeName.exe \path\to\directoryA \path\to\directoryB.
In the Visual Basic example, the code explicitly sets the MTAThread attribute on the Main() method.
File synchronization provider requires applications to use the multithreaded apartment (MTA) threading model.
How to: Synchronize Files by Using Managed Code的更多相关文章
- Optimize Managed Code For Multi-Core Machines
Parallel Performance Optimize Managed Code For Multi-Core Machines Daan Leijen and Judd Hall This ar ...
- Passing JavaScript Objects to Managed Code
Silverlight If the target managed property or input parameter is strongly typed (that is, not typed ...
- 不要忽视Managed code stripping的副作用
0x00 前言 Unity 2018.3之后,新的“Managed Stripping Level”选项将替换 player settings 中原有的“Stripping Level”选项. 这个新 ...
- C#中的文件同步
How to: Synchronize Files by Using Managed Code FileSyncProvider Class File Synchronization Provider ...
- Oracle管理文件OMF (oracle managed files)
简化dba的管理操作 1:启用 omf 23:16:04 SYS@orcl> show parameter DB_CREATE_FILE_DEST NAME TYPE VALUE ------- ...
- [转]Passing Managed Structures With Strings To Unmanaged Code Part 3
1. Introduction. 1.1 In part 1 of this series of blogs we studied how to pass a managed structure (w ...
- [转]Passing Managed Structures With Strings To Unmanaged Code Part 2
1. Introduction. 1.1 In part 1 of this series of blogs we studied how to pass a managed structure (w ...
- [转]Passing Managed Structures With Strings To Unmanaged Code Part 1
1. Introduction. 1.1 Managed structures that contain strings are a common sight. The trouble is that ...
- Getting Started Synchronizing Files
https://msdn.microsoft.com/en-US/library/bb902813(v=sql.110).aspx Sync Framework includes a file syn ...
随机推荐
- 【BZOJ】【3613】【HEOI2014】南园满地堆轻絮
思路题 考试结束前5.6min的时候想到……但是写挂了QAQ 其实就是(差值最大的逆序对之差+1)/2; 找逆序对其实维护一个max直接往过扫就可以了……因为逆序对是前面的数大于后面的数…… 正确性显 ...
- 【BZOJ】【3282】Tree
LCT 喜闻乐见的Link-Cut-Tree…… srO zyf http://www.cnblogs.com/zyfzyf/p/4149109.html 目测我是第222个?………………不要在意这些 ...
- Model-View-Controller - 杂谈
经典的B/S结构+MVC模式: 注意:MVC层次均位于server中. 下面通过一个简单并且具体的例子来体会下这种MVC模式: --- 功能:查询所有图书:涉及到数据库,JSP,Servlet等 S ...
- 剑指offer--7题
*题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. *句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. *例如输入“I am a student.”,则输出“st ...
- phonegap上传以及下载图片
在phonegap中,有时我们需要从服务器下载图片以及上传图片,这个时候可以用到官方提供的一个插件:FileTransfer 首先通过命令添加插件: cordova plugin add org.ap ...
- php正则过滤html标签、空格、换行符的代码,提取图片
$descclear = str_replace("r","",$descclear);//过滤换行 $descclear = str_replace(&quo ...
- Silverlight编程模型
Silverlight支持Javascript API编程模型和托管API编程模型这两种编程模型,它们的基本作用都是用于XAML界面文件中的XAML对象,基于托管API编程的XAML应用程序是通过x: ...
- awk 统计数据在文件中的出现次数
突然发现awk原来可以统计同一数据在要处理的文件中所出现的次数.原来的时候为了分析数据还自己写程序,哎,无语,当时还以为自己多强,手工分析不过来的东西写程序处理.现在想来实在是年少轻狂.解决问题嘛,不 ...
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: ja ...
- close和shutdown的区别
转的,没验证 close(sock_fd)会把sock_fd的内部计数器减1当sock_fd的内部计数器为0时, 才调用shutodwn(), 并最终释放文件描述符调用shutdown()只是进行了T ...