/// <summary> /// Translates the system event to FileChangedEvent. /// </summary> /// <returns>The event.</returns> /// <param name="e">The event args.</param> private FileChangedEventArgs TranslateEvent(FileSystemEventArgs e) { FileChangedEventArgs newEvent = new FileChangedEventArgs(); newEvent.ChangeType = e.ChangeType; newEvent.FullPath = e.FullPath.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); newEvent.Name = e.Name.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); if (this.LastEvent.Ticks >= DateTime.Now.Ticks) { newEvent.When = this.LastEvent.AddTicks(1); } else { newEvent.When = DateTime.Now; } this.LastEvent = newEvent.When; if (e.ChangeType != WatcherChangeTypes.Deleted) { FileInfo info = new FileInfo(newEvent.FullPath); newEvent.IsDirectory = (info.Attributes & FileAttributes.Directory) == FileAttributes.Directory; } if (e.ChangeType == WatcherChangeTypes.Renamed) { newEvent.OldName = ((RenamedEventArgs)e).OldName.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); newEvent.OldFullPath = ((RenamedEventArgs)e).OldFullPath.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); } return(newEvent); }
/// <summary> /// Handle create event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event.</param> private void OnCreatedEvent(object sender, FileSystemEventArgs e) { FileChangedEventArgs newEvent = this.TranslateEvent(e); if (this.Created != null) { this.Created(newEvent); } }
/// <summary> /// Handle delete event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event.</param> private void OnDeletedEvent(object sender, FileSystemEventArgs e) { Console.WriteLine("Deleted: {0}", e.Name); FileChangedEventArgs newEvent = this.TranslateEvent(e); if (this.Deleted != null) { this.Deleted(newEvent); } }
/// <summary> /// Handle the changed event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event.</param> private void OnChangedEvent(object sender, FileSystemEventArgs e) { Console.WriteLine("Changed: {0}", e.Name); FileChangedEventArgs newEvent = this.TranslateEvent(e); if (!newEvent.IsDirectory) { newEvent.SHA1 = CommonHelper.GetSHA1Hash(e.FullPath); newEvent.DataPath = Config.MetaFolderData.File(newEvent.SHA1); if (!File.Exists(newEvent.DataPath)) { File.Copy(newEvent.FullPath, newEvent.DataPath); } } if (this.Changed != null) { this.Changed(newEvent); } }
/// <summary> /// Handle rename event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The event.</param> private void OnRenamedEvent(object sender, RenamedEventArgs e) { Console.WriteLine("Renamed: {0} -> {1}", e.OldName, e.Name); FileChangedEventArgs newEvent = this.TranslateEvent(e); if (!newEvent.IsDirectory) { // TODO remove sha1 newEvent.SHA1 = CommonHelper.GetSHA1Hash(e.FullPath); newEvent.DataPath = Config.MetaFolderData.File(newEvent.SHA1); if (!File.Exists(newEvent.DataPath)) { File.Copy(newEvent.FullPath, newEvent.DataPath); } } if (this.Renamed != null) { this.Renamed(newEvent); } }
/// <summary> /// Translates the system event to FileChangedEvent. /// </summary> /// <returns>The event.</returns> /// <param name="e">The event args.</param> private FileChangedEventArgs TranslateEvent(FileSystemEventArgs e) { FileChangedEventArgs newEvent = new FileChangedEventArgs(); newEvent.ChangeType = e.ChangeType; newEvent.FullPath = e.FullPath.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); newEvent.Name = e.Name.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); if (this.LastEvent.Ticks >= DateTime.Now.Ticks) { newEvent.When = this.LastEvent.AddTicks(1); } else { newEvent.When = DateTime.Now; } this.LastEvent = newEvent.When; if (e.ChangeType != WatcherChangeTypes.Deleted) { FileInfo info = new FileInfo(newEvent.FullPath); newEvent.IsDirectory = (info.Attributes & FileAttributes.Directory) == FileAttributes.Directory; } if (e.ChangeType == WatcherChangeTypes.Renamed) { newEvent.OldName = ((RenamedEventArgs)e).OldName.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); newEvent.OldFullPath = ((RenamedEventArgs)e).OldFullPath.Replace("\\", Properties.PathSep).Replace("/", Properties.PathSep); } return newEvent; }
/// <summary> /// File renamed. /// </summary> /// <param name="e">The event.</param> public void Renamed(FileChangedEventArgs e) { Console.WriteLine("Renamed"); VersionList.Rename(e.Name, e.OldName, e.SHA1, e.When); }
/// <summary> /// File changed. /// </summary> /// <param name="e">The event.</param> public void Changed(FileChangedEventArgs e) { Console.WriteLine("Changed"); VersionList.Change(e.Name, e.IsDirectory, e.SHA1, e.When); }
/// <summary> /// File created. /// </summary> /// <param name="e">The event.</param> public void Created(FileChangedEventArgs e) { // TODO comments Console.WriteLine("Created"); VersionList.Create(e.Name, e.IsDirectory, e.When); }
/// <summary> /// File deleted. /// </summary> /// <param name="e">The event.</param> public void Deleted(FileChangedEventArgs e) { Console.WriteLine("Deleted"); VersionList.Delete(e.Name, e.When); }