/// <summary> /// 监视OnRenamed事件 /// </summary> /// <param name="source"></param> /// <param name="e"></param> void OnRenamed(object source, RenamedEventArgs e) { FileSystemWatcher watcher = source as FileSystemWatcher; SyncAction syncItem = new SyncAction(); syncItem.OldFullPath = e.OldFullPath; syncItem.FullPath = e.FullPath; syncItem.ChangedTime = DateTime.Now; syncItem.Name = e.Name; syncItem.OldName = e.OldName; syncItem.ChangedType = e.ChangeType; syncItem.SyncPath = watcher.Path; if (syncItem.FullPath.ToLower().EndsWith(".tmp")) return; this.safeQueue.Enqueue(syncItem); //入队列 }
/// <summary> /// 同步 /// </summary> /// <param name="item"></param> private void SyncProcess(SyncAction item) { string logicFullPath = string.Empty; //同步watchList下的全部目录 foreach (string path in this.watchList) { if (item.FullPath.StartsWith(path)) continue; //除去同步目录的全路径 logicFullPath = item.FullPath.Replace(item.SyncPath, ""); string fullPath = path + logicFullPath; switch (item.ChangedType) { //-----------Created ------------ case WatcherChangeTypes.Created: //复制文件 if (FilesIsUsing(item.FullPath, fullPath, true)) return; if (IsDirectory(item.FullPath)) { if (!IsDirectory(fullPath)) //创建||复制目录 CopyDirectory(item.FullPath, path, item.SyncPath); } else if (IsFile(item.FullPath)) { CopyFile(item.FullPath, fullPath, item.SyncPath, false); } break; //-----------------&& Changed------------- case WatcherChangeTypes.Changed: //检查文件是否在使用 if (FilesIsUsing(item.FullPath, fullPath, true)) return; //规范出队顺序-》重命名- 修改- 删除 foreach (SyncAction ac in safeQueue.ToArray()) { if (ac.ChangedType == WatcherChangeTypes.Renamed) { safeQueue.TryDequeue(out item); safeQueue.Enqueue(item); return; } } if (IsDirectory(item.FullPath)) { if (!IsDirectory(fullPath)) //在同步目录下是否存在了? CopyDirectory(item.FullPath, path, item.SyncPath); } else if (IsFile(item.FullPath)) { if (!CopyFile(item.FullPath, fullPath, item.SyncPath, true)) { return; } } break; //---------------&& Deleted---------- case WatcherChangeTypes.Deleted: foreach (SyncAction sa in safeQueue.ToArray()) { if (sa.ChangedType != WatcherChangeTypes.Deleted) { safeQueue.TryDequeue(out item); safeQueue.Enqueue(item); return; } } if (FilesIsUsing(item.FullPath, fullPath, true)) return; while (true) { try { if (renameRecord.Keys.Contains(item.FullPath)) { Delete(renameRecord[item.FullPath]); renameRecord.Remove(item.FullPath); } Delete(fullPath); break; } catch (Exception ex) { Console.WriteLine("Delete Error :" + ex.Message); } } break; //------------&& Renamed------------- case WatcherChangeTypes.Renamed: string oldLogicFullDir = string.Empty; string newLogicFullDir = string.Empty; oldLogicFullDir = item.OldFullPath.Replace(item.SyncPath, ""); newLogicFullDir = item.FullPath.Replace(item.SyncPath, ""); string oldFullDir = path + oldLogicFullDir; string newFullDir = path + newLogicFullDir; //保证新路径 旧路径文件都没用户访问 if (FilesIsUsing(item.FullPath, fullPath, true) && FilesIsUsing(item.OldFullPath, path + "\\" + item.OldName, true)) return; //把重命名路径 在Queue 中对应的路径改了 foreach (SyncAction ac in safeQueue.ToArray()) { if (ac.ChangedType == WatcherChangeTypes.Changed) { if (ac.FullPath == item.OldFullPath) ac.FullPath = item.FullPath; } } // Rename DirectoryName if (this.IsDirectory(item.FullPath) && !Directory.Exists(newFullDir)) { DirectoryInfo dirDst = new DirectoryInfo(oldFullDir); if (Directory.Exists(oldFullDir)) { dirDst.MoveTo(newFullDir); } else { CopyDirectory(item.FullPath, path, item.SyncPath); } } //Rename FileName else if (IsFile(item.FullPath) && !File.Exists(newFullDir)) { FileInfo fileDst = new FileInfo(oldFullDir); if (File.Exists(oldFullDir)) { fileDst.MoveTo(newFullDir); } else { CopyFile(item.FullPath, fullPath, item.SyncPath, false); } } if (!renameRecord.Keys.Contains(item.FullPath)) renameRecord.Add(item.FullPath, oldFullDir); // 删除旧路径重新进队 safeQueue.Enqueue(new SyncAction() { ChangedType = WatcherChangeTypes.Deleted, FullPath = item.OldFullPath, SyncPath = item.SyncPath }); break; default: break; } safeQueue.TryDequeue(out currentItem); //出列 Console.WriteLine("同步完成:" + item.ChangedType.ToString() + "--" + fullPath); Console.WriteLine(" " + item.FullPath); } }
/// <summary> /// 监视的Onchang事件 /// </summary> /// <param name="source"></param> /// <param name="e"></param> void OnChanged(object source, FileSystemEventArgs e) { if (!this.IsFile(e.FullPath) && e.ChangeType == WatcherChangeTypes.Changed) return; FileSystemWatcher watcher = source as FileSystemWatcher; SyncAction syncItem = new SyncAction(); syncItem.FullPath = e.FullPath; syncItem.ChangedTime = DateTime.Now; syncItem.Name = e.Name; syncItem.ChangedType = e.ChangeType; syncItem.SyncPath = watcher.Path; string logicFullPath = string.Empty; //path 目标路径 foreach (string path in watchList) { if (syncItem.FullPath.StartsWith(path)) continue; logicFullPath = syncItem.FullPath.Replace(syncItem.SyncPath, ""); string fullPath = path + logicFullPath; switch (syncItem.ChangedType) { case WatcherChangeTypes.Created: if (File.Exists(fullPath) || Directory.Exists(fullPath)) { return; //已存在 不加入队列 } break; case WatcherChangeTypes.Changed: if (IsFile(syncItem.FullPath) && IsFile(fullPath)) { FileInfo srcFile = new FileInfo(syncItem.FullPath); FileInfo dstFile = new FileInfo(fullPath); if (srcFile.LastWriteTime <= dstFile.LastWriteTime) return; } break; default: break; } } //同步实体入队列 if (syncItem.FullPath.ToLower().EndsWith(".tmp")) return; this.safeQueue.Enqueue(syncItem); }