using System;
using System.IO;
namespace QueueSample
{
class Program
{
static void Main(string[] args)
{
MyFileSystemWather myWather = new MyFileSystemWather(@"C:\test", "*.txt");
myWather.OnChanged += new FileSystemEventHandler(OnChanged);
myWather.OnCreated += new FileSystemEventHandler(OnCreated);
myWather.OnRenamed += new RenamedEventHandler(OnRenamed);
myWather.OnDeleted += new FileSystemEventHandler(OnDeleted);
myWather.Start();
//由于是控制台程序,加个输入避免主线程执行完毕,看不到监控效果
Console.ReadKey();
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("文件新建事件处理逻辑");
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("文件改变事件处理逻辑");
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
Console.WriteLine("文件删除事件处理逻辑");
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("文件重命名事件处理逻辑");
}
}
}
using System.IO;
namespace QueueSample
{
public class WatcherProcess
{
private object sender;
private object eParam;
public event RenamedEventHandler OnRenamed;
public event FileSystemEventHandler OnChanged;
public event FileSystemEventHandler OnCreated;
public event FileSystemEventHandler OnDeleted;
public event Completed OnCompleted;
public WatcherProcess(object sender, object eParam)
{
this.sender = sender;
this.eParam = eParam;
}
public void Process()
{
if (eParam.GetType() == typeof(RenamedEventArgs))
{
OnRenamed(sender, (RenamedEventArgs)eParam);
OnCompleted(((RenamedEventArgs)eParam).FullPath);
}
else
{
FileSystemEventArgs e = (FileSystemEventArgs)eParam;
if (e.ChangeType == WatcherChangeTypes.Created)
{
OnCreated(sender, e);
OnCompleted(e.FullPath);
}
else if (e.ChangeType == WatcherChangeTypes.Changed)
{
OnChanged(sender, e);
OnCompleted(e.FullPath);
}
else if (e.ChangeType == WatcherChangeTypes.Deleted)
{
OnDeleted(sender, e);
OnCompleted(e.FullPath);
}
else
{
OnCompleted(e.FullPath);
}
}
}
}
}
using System;
using System.Collections;
using System.IO;
using System.Threading;
namespace QueueSample
{
public delegate void Completed(string key);
public class MyFileSystemWather
{
private FileSystemWatcher fsWather;
private Hashtable hstbWather;
public event RenamedEventHandler OnRenamed;
public event FileSystemEventHandler OnChanged;
public event FileSystemEventHandler OnCreated;
public event FileSystemEventHandler OnDeleted;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="path">要监控的路径</param>
public MyFileSystemWather(string path, string filter)
{
if (!Directory.Exists(path))
{
throw new Exception("找不到路径:" + path);
}
hstbWather = new Hashtable();
fsWather = new FileSystemWatcher(path);
// 是否监控子目录
fsWather.IncludeSubdirectories = false;
fsWather.Filter = filter;
fsWather.Renamed += new RenamedEventHandler(fsWather_Renamed);
fsWather.Changed += new FileSystemEventHandler(fsWather_Changed);
fsWather.Created += new FileSystemEventHandler(fsWather_Created);
fsWather.Deleted += new FileSystemEventHandler(fsWather_Deleted);
}
/// <summary>
/// 开始监控
/// </summary>
public void Start()
{
fsWather.EnableRaisingEvents = true;
}
/// <summary>
/// 停止监控
/// </summary>
public void Stop()
{
fsWather.EnableRaisingEvents = false;
}
/// <summary>
/// filesystemWatcher 本身的事件通知处理过程
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void fsWather_Renamed(object sender, RenamedEventArgs e)
{
lock (hstbWather)
{
hstbWather.Add(e.FullPath, e);
}
WatcherProcess watcherProcess = new WatcherProcess(sender, e);
watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
watcherProcess.OnRenamed += new RenamedEventHandler(WatcherProcess_OnRenamed);
Thread thread = new Thread(watcherProcess.Process);
thread.Start();
}
private void WatcherProcess_OnRenamed(object sender, RenamedEventArgs e)
{
OnRenamed(sender, e);
}
private void fsWather_Created(object sender, FileSystemEventArgs e)
{
lock (hstbWather)
{
hstbWather.Add(e.FullPath, e);
}
WatcherProcess watcherProcess = new WatcherProcess(sender, e);
watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
watcherProcess.OnCreated += new FileSystemEventHandler(WatcherProcess_OnCreated);
Thread threadDeal = new Thread(watcherProcess.Process);
threadDeal.Start();
}
private void WatcherProcess_OnCreated(object sender, FileSystemEventArgs e)
{
OnCreated(sender, e);
}
private void fsWather_Deleted(object sender, FileSystemEventArgs e)
{
lock (hstbWather)
{
hstbWather.Add(e.FullPath, e);
}
WatcherProcess watcherProcess = new WatcherProcess(sender, e);
watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
watcherProcess.OnDeleted += new FileSystemEventHandler(WatcherProcess_OnDeleted);
Thread tdDeal = new Thread(watcherProcess.Process);
tdDeal.Start();
}
private void WatcherProcess_OnDeleted(object sender, FileSystemEventArgs e)
{
OnDeleted(sender, e);
}
private void fsWather_Changed(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Changed)
{
if (hstbWather.ContainsKey(e.FullPath))
{
WatcherChangeTypes oldType = ((FileSystemEventArgs)hstbWather[e.FullPath]).ChangeType;
if (oldType == WatcherChangeTypes.Created || oldType == WatcherChangeTypes.Changed)
{
return;
}
}
}
lock (hstbWather)
{
hstbWather.Add(e.FullPath, e);
}
WatcherProcess watcherProcess = new WatcherProcess(sender, e);
watcherProcess.OnCompleted += new Completed(WatcherProcess_OnCompleted);
watcherProcess.OnChanged += new FileSystemEventHandler(WatcherProcess_OnChanged);
Thread thread = new Thread(watcherProcess.Process);
thread.Start();
}
private void WatcherProcess_OnChanged(object sender, FileSystemEventArgs e)
{
OnChanged(sender, e);
}
public void WatcherProcess_OnCompleted(string key)
{
lock (hstbWather)
{
hstbWather.Remove(key);
}
}
}
}
using Microsoft.Synchronization;
using Microsoft.Synchronization.Files;
using System;
using System.IO;
using System.Xml.Linq;
namespace QueueSample
{
class Program
{
/// <summary>
/// 文件监控Microsoft.Synchronization
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
/* 存放需要同步的文件夹,以后其他场景临时使用一下.
<?xml version="1.0" encoding="utf-8" ?>
<items>
<item from="C:\\test" to="test1" />
</items>
*/
XElement doc = XElement.Load("File.xml");
var itms = doc.Descendants("item");
foreach (var item in itms)
{
var from = item.Attribute("from").Value;
var to = item.Attribute("to").Value;
if (!string.IsNullOrEmpty(from) && !string.IsNullOrEmpty(to) && Directory.Exists(from))
{
if (!Directory.Exists(to))
{
Directory.CreateDirectory(to);
}
//来源文件夹
FileSyncProvider sourceProvider = new FileSyncProvider(Guid.NewGuid(), from);
//目录文件夹
FileSyncProvider destProvider = new FileSyncProvider(Guid.NewGuid(), to);
SyncOrchestrator syncAgent = new SyncOrchestrator();
syncAgent.LocalProvider = sourceProvider;
syncAgent.RemoteProvider = destProvider;
syncAgent.Synchronize();
Console.WriteLine(from + " 到 " + to + " 同步完成");
//文件夹监控
//MyFileSystemWather myWather = new MyFileSystemWather(@"C:\test", "*.txt");
//myWather.OnChanged += new FileSystemEventHandler(OnChanged);
//myWather.OnCreated += new FileSystemEventHandler(OnCreated);
//myWather.OnRenamed += new RenamedEventHandler(OnRenamed);
//myWather.OnDeleted += new FileSystemEventHandler(OnDeleted);
//myWather.Start();
FileSystemWatcher watcher = new FileSystemWatcher(from, "*.*");
watcher.Changed += (O, E) =>
{
//Console.WriteLine(DateTime.Now.ToString() + E.ChangeType);
//启动同步回话
syncAgent.Synchronize();
Console.WriteLine(from + " 到 " + to + " 同步完成");
};
watcher.EnableRaisingEvents = true;
}
}
Console.ReadLine();
}
}
}