public virtual void InvokeEditedEvent(BackupObject obj) { if (Edited != null) { Edited.Invoke(obj); } }
private void LoadObjects() { try { using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open))) { while (reader.PeekChar() > -1) { BackupObject obj = new BackupObject(); obj.Name = reader.ReadString(); obj.FromPath = reader.ReadString(); obj.ToPath = reader.ReadString(); objects.Add(obj); } } } catch (FileNotFoundException) { Logger.RecordMessageToLog("File not found: " + path); } catch (LoadDataException) { Logger.RecordMessageToLog("Can't load data from file: " + path); } catch (Exception e) { Logger.RecordMessageToLog(e.Message); } }
public virtual void InvokeDeletedEvent(BackupObject obj) { if (Deleted != null) { Deleted.Invoke(obj); } }
public virtual void InvokeAddedEvent(BackupObject obj) { if (Added != null) { Added.Invoke(obj); } }
void EditObject(BackupObject obj) { if (!File.Exists(obj.FromPath)) { BackupObject search = directoryObjects.First(o => o.FromPath == obj.FromPath); if (search != null) { search = obj; FileSystemWatcher searchW = watchers.First(w => w.Path == obj.FromPath); if (searchW != null) { try { var newWatcher = new FileSystemWatcher(obj.FromPath); SetWatcherOptions(newWatcher); searchW = newWatcher; } catch (Exception ex) { Logger.RecordMessageToLog(ex.Message); } } } } else { filesObjects.Add(obj); } }
public void AddBackupObject(BackupObject obj) { if (obj != null && objects.Find(o => o.FromPath == obj.FromPath) == null) { objects.Add(obj); SaveObjects(); } }
public void Edit(BackupObject obj) { BackupObject searchObj = objects.Find(o => obj.FromPath == o.FromPath); if (searchObj != null) { searchObj = obj; SaveObjects(); } }
public void DeleteObject(BackupObject obj) { BackupObject search = objects.First(o => o.FromPath == obj.FromPath); if (search != null) { objects.Remove(obj); SaveObjects(); } }
public override void DeleteObjectBackup(BackupObject delete) { BackupObject obj = repository.GetList().First(o => o.FromPath == delete.FromPath); if (obj != null) { repository.DeleteObject(obj); base.InvokeDeletedEvent(obj); } }
public void CopyDirectory(object backupObject) { BackupObject obj = (BackupObject)backupObject; DirectoryInfo fromDirectory = new DirectoryInfo(obj.FromPath); DirectoryInfo toDirectory = new DirectoryInfo(obj.ToPath); //проверяем на наличие исходной директории if (!fromDirectory.Exists) { return; } else { //проверяем на наличие директории, в которую будем копировать файлы; если нет-создаем if (!toDirectory.Exists) { toDirectory.Create(); } else { //удаляем файлы и папки, которых уже нет в исходной папке if (toDirectory.GetFiles().Length > 0) { DeleteFiles(fromDirectory, toDirectory); } if (toDirectory.GetDirectories().Length > 0) { DeleteDirectories(fromDirectory, toDirectory); } } FileInfo[] files = fromDirectory.GetFiles(); if (files.Length > 0) { CopyFiles(files, toDirectory); } DirectoryInfo[] directories = fromDirectory.GetDirectories(); if (directories.Length > 0) { foreach (var item in directories) { DirectoryInfo toChildDirectory = new DirectoryInfo(Path.Combine(toDirectory.FullName, item.Name)); BackupObject subObj = new BackupObject { FromPath = item.FullName, ToPath = Path.Combine(toDirectory.FullName, item.Name) }; CopyDirectory(subObj); } } } }
//обработчик события - добавления в репозиторий нового обьекта для рез.коп. и добавление его в локальный список void AddNewObject(BackupObject obj) { if (!File.Exists(obj.FromPath)) { directoryObjects.Add(obj); FileSystemWatcher watcher = new FileSystemWatcher(obj.FromPath); SetWatcherOptions(watcher); watchers.Add(watcher); } else { filesObjects.Add(obj); } }
public override void EditObjectBackup(string fromPath, string toPath) { if ((Directory.Exists(fromPath) && Directory.Exists(toPath)) || (File.Exists(fromPath) && File.Exists(toPath))) { BackupObject obj = new BackupObject { FromPath = fromPath, ToPath = toPath }; repository.Edit(obj); base.InvokeEditedEvent(obj); } }
public override void StartBackup(BackupObject obj) { Thread thread; if (Directory.Exists(obj.FromPath)) { thread = new Thread(new ParameterizedThreadStart(new Backuper().CopyDirectory)); thread.Start(obj); } else if (File.Exists(obj.FromPath)) { thread = new Thread(new ParameterizedThreadStart(new Backuper().CopyFile)); thread.Start(obj); } }
//обработчик события - удаление обьекта из репозитория void DeleteObject(BackupObject obj) { if (!File.Exists(obj.FromPath)) { directoryObjects.Remove(obj); FileSystemWatcher search = watchers.First(w => w.Path == obj.FromPath); if (search != null) { search.EnableRaisingEvents = false; watchers.Remove(search); } } else { filesObjects.Add(obj); } }
public void CopyFile(object backupObject) { try { BackupObject fileBackup = (BackupObject)backupObject; FileInfo file = new FileInfo(fileBackup.FromPath); DirectoryInfo toDirectory = new DirectoryInfo(fileBackup.ToPath); File.Copy(file.FullName, Path.Combine(toDirectory.FullName, file.Name), true); } catch (FileNotFoundException ex) { throw ex; } catch (Exception ex) { throw ex; } }
public override void AddObjectBackup(string name, string fromPath, string toPath) { if ((Directory.Exists(fromPath) && Directory.Exists(toPath)) || (File.Exists(fromPath) && Directory.Exists(toPath))) { BackupObject obj = new BackupObject { Name = name, FromPath = fromPath, ToPath = toPath }; repository.AddBackupObject(obj); base.InvokeAddedEvent(obj); } else { throw new WrongPathException(); } }
//обработчик события, который ловит любые изменения в обьекте рез.коп. void watcher_Changed(object sender, FileSystemEventArgs e) { //ловим только те изменения, которые произошли на уровне директории или файла в корневой директории //if (Directory.Exists(e.FullPath)|| // (File.Exists(e.FullPath)&& // objects.Find(o=>o.FromPath== // new FileInfo(e.FullPath).Directory.FullName)!=null)) //ищем нужный обьект, исходный путь которого входит в путь текущего измененного обьекта BackupObject search = directoryObjects.First(o => e.FullPath.Contains(o.FromPath)); if (search != null) { Logger.RecordEventToLog(search.Name, e.ChangeType.ToString(), e.FullPath); try { controller.StartBackup(search); } catch (Exception ex) { Logger.RecordMessageToLog(ex.Message); } } }
public abstract void DeleteObjectBackup(BackupObject obj);
public abstract void StartBackup(BackupObject obj);